iOS qq表情(发送信息页面获得表情页面的表情标签)页面之间的跳转

 

1.在名称为ZLViewController的xib文件中有如下控件:

    

 在名称为FaceViewController的xib文件中有如下控件:

2.点击ZLViewController中的button按钮跳转到FaceViewController页面函数如下:

- (IBAction)image:(id)sender {

    self.messageString=[NSMutableStringstringWithFormat:@"%@",self.messageTextField.text];

    if (self.faceViewController==nil) {

         self.faceViewController=[[FaceViewControlleralloc]initWithNibName:@"FaceViewController"bundle:nil];

    }

    [selfpresentViewController:self.faceViewControlleranimated:YEScompletion:nil];

}

3.点击FaceViewController中的button按钮跳转到ZLViewController页面并且在文本框中显示FaceViewController中的button的标签值函数如下:

-(void)didSelectAFace:(id)sender

{

    UIButton *tempBtn=(UIButton *)sender;

    NSMutableDictionary *tempDicImage=[imageArry objectAtIndex:tempBtn.tag];

    NSArray *tempArray=[tempDicImage allKeys];

    NSString *faceStr=[NSString stringWithFormat:@"%@",[tempArray objectAtIndex:0]];

    [self.zlViewController.messageStringappendString:faceStr];

    [self dismissViewControllerAnimated:YEScompletion:nil];

运行这行代码的时[self dismissViewControllerAnimated:YEScompletion:nil];回去调用ZLViewController类里面的函数

-(void)viewWillAppear:(BOOL)animated{

[superviewWillAppear:YES];

 self.title = @"IM通信";

 self.messageTextField.text=self.messageString;

}使ZLViewController类的xib中的文本框显示FaceViewController类按钮标签的值。

4.但是在ZLViewController类中.m文件中的- (void)viewDidLoad函数里面添加self.faceViewController.zlViewController=self;此行代码是很关键的,如果没有添加在文本框中就不能得到按钮的标签值。其中faceViewController是FaceViewController的对象,并且是属于ZLViewController类中的属性字段。 zlViewController是ZLViewController类的对象,并且是属于    FaceViewController类中的属性字段。并且在类FaceViewController中的- (void)viewDidLoad函数中不能添加self.zlViewController=[[ZLViewControlleralloc]init];这行代码,有这行代码也会出现跳回ZLViewController类新建的对象,不是原来的对象。

 

 

5.FaceViewController是表情类,通过Button在Scrollview上面添加表情如:

在FaceViewController类.m文件中有如下:

#import "FaceViewController.h"
#import "ZLViewController.h"

@interface FaceViewController ()
{
    //存放表情的动态数组
    NSMutableArray *imageArry;
    
    
}

@end

@implementation FaceViewController
@synthesize faceScrolView,zlViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //用于设置表情位置
    int xIndex=0;
    int yIndex=0;
    
    imageArry=[[NSMutableArray alloc]init];
    
    //给动态数组添加105张表情和建立105个按钮
    for (int i=0; i<105; i++) {
    
        UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        
        //定义一个动态字典
        NSMutableDictionary *dicImage=[NSMutableDictionary dictionary];
        
        //设置字典的键和值
        [dicImage setValue:image forKey:[NSString stringWithFormat:@"[/%d]",i]];
        
        //把字典加入多数组中
        [imageArry addObject:dicImage];
 
        
        
        //定义一个按钮
        UIButton  *button=[UIButton buttonWithType:UIButtonTypeCustom];
        
        //给按钮设置frame,即按钮的起点和大小
        button.frame=CGRectMake(10+xIndex*32, 10+yIndex*32, 32.0f, 32.0f);
        
        //给按钮设置背景图片
        [button setBackgroundImage:image forState:UIControlStateNormal];
        
        //给按钮添加标签
        button.tag=i;
        
        //当点击按钮时,给按钮添加行为(即方法didSelectAFace)
        [button addTarget:self action:@selector(didSelectAFace:) forControlEvents:UIControlEventTouchUpInside];
      
        //把按钮添加到ScrolView控件的对象控件中
        [faceScrolView addSubview:button];
        
        xIndex+=1;
        
        //每一行添加9张图片,当满9张时,跳到下一排
        if (xIndex==9) {
            
            xIndex=0;
            yIndex+=1;
        } 
        
    }
    
    //给ScrolView控件的对象faceScrolView控件设置大小
    [faceScrolView setContentSize:CGSizeMake(300.0f, 12+(yIndex+1)*32)];
    
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


//点击按钮时的响应函数
-(void)didSelectAFace:(id)sender
{
   //接收被点击的按钮
    UIButton *tempBtn=(UIButton *)sender;
    
    //根据被点击按钮的标签获得动态数组中的对象
    NSMutableDictionary *tempDicImage=[imageArry objectAtIndex:tempBtn.tag];
    
    //获得动态数组中具体对象的键值
    NSArray *tempArray=[tempDicImage allKeys];
    NSString *faceStr=[NSString stringWithFormat:@"%@",[tempArray objectAtIndex:0]];
  
    [self.zlViewController.messageString appendString:faceStr];
   
    //隐藏本类的xib,回到上一级的xib
    [self dismissViewControllerAnimated:YES completion:nil];
}






@end

 

posted on 2013-10-09 16:26  liukunyang  阅读(511)  评论(0编辑  收藏  举报

导航