将多个值传递给nextview控制器

时间:2013-05-04 05:35:25

标签: ios objective-c

我正在开发一个应用程序,其中有三个不同的表和单个UiView,其中包含标签。我想显示所选表格视图单元名称的名称,将其设置为标签。

    (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    AppDelegate * appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    if(appdelegate.btn1)
    {
        appdelegate.selectId=indexPath.row;
        NSLog(@"select Id %d",appdelegate.selectId);    
    }
    else if(appdelegate.btn2)
    {
        appdelegate.selectId=indexPath.row;
        NSLog(@"select Id %d",appdelegate.selectId);
    }
    else if(appdelegate.btn3)
    {
        appdelegate.selectId=indexPath.row;
        NSLog(@"select Id %d",appdelegate.selectId);
    }

    NSString * str=[appdelegate.name objectAtIndex:indexPath.row];
    NSString * str1=[appdelegate.iname objectAtIndex:indexPath.row];
    NSString * str2=[appdelegate.bname objectAtIndex:indexPath.row];

    DetailViewController * dvc=[[DetailViewController alloc]initWithTrick:str andOtherString:str1 andOtherString:str2];

    [self.navigationController pushViewController:dvc animated:YES];

}

将该单元格名称访问为init方法。

 -(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;
{
       if (self)
       {

           self.trickname=name_;
           self.trickname2=name2_;
           self.trickname3=name3_;

    }
    return self;


  }

这里的trickname,trickname2,trickname3是NSString。

我已经尝试过这段代码但不适合我。

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

 -(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;
{ 
  self =[super init]; 
   if (self)
   {

       self.trickname=name_;
       self.trickname2=name2_;
       self.trickname3=name3_;

}
return self;
}
试试这个可能对你有所帮助。您的自我对象未被初始化,因此未在字符串中设置值。

答案 1 :(得分:0)

你可以这样做:

DetailViewController.h类中创建三个属性字符串:

@property (strong, nonatomic) NSString *name1;
@property (strong, nonatomic) NSString *name2;
@property (strong, nonatomic) NSString *name3;

然后将DetailViewController.m类中的这些同义为:

@synthesize name1,name2,name3;

然后在tableview委托didselectrow中访问这些

    DetailViewController * dvc=[[DetailViewController alloc]initWithTrick:str andOtherString:str1 andOtherString:str2];
dvc.name1= str;
dvc.name2= str1;
dvc.name3= str2;

    [self.navigationController pushViewController:dvc animated:YES];

然后在init方法中访问它们。

希望它对你有所帮助。

答案 2 :(得分:0)

保留这一切,并将此值传递给Detailview

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {   
        //do what you did that app delegate thing 

        //now we will pass value which you got from doing stuff above
        //Initialize the detail view controller and display it.
        DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
         // i have just put DetailViewController here as nibname you give name of your detailviewcontrollers XIB

        dvController.trickname = string1;
        dvController.trickname2 = string2;
        dvController.trickname3 =string3;

        // you are passing values of string 1 2 and 3 to detail view controller string trickname, trickname2 and 3

        [self.navigationController pushViewController:dvController animated:YES];
        [dvController release];

    }

注意:trickname,trickname2,trickname3需要在.h文件中的详细视图控制器中声明

答案 3 :(得分:0)

我现在解决了我的问题。

在表视图控制器中声明该字符串

{
    NSString * str;
    NSString * str1;
    NSString * str2;
}
@property(strong,nonatomic)NSString * str;
@property(strong,nonatomic)NSString * str1;
@property(strong,nonatomic)NSString * str2;

在表视图控制器的.m中合成。

 @synthesize str,str1,str2;

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {


        AppDelegate * appdelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

        if(appdelegate.btn1)
        {
            appdelegate.selectId=indexPath.row;
            NSLog(@"select Id %d",appdelegate.selectId);

            str=[appdelegate.name objectAtIndex:indexPath.row];

         }

        else if(appdelegate.btn2)
        {
            appdelegate.selectId=indexPath.row;

            NSLog(@"select Id %d",appdelegate.selectId);


            str1=[appdelegate.iname objectAtIndex:indexPath.row];

        }

        else if(appdelegate.btn3)
        {

            appdelegate.selectId=indexPath.row;

            NSLog(@"select Id %d",appdelegate.selectId);

                str2=[appdelegate.bname objectAtIndex:indexPath.row];

        }

    DetailViewController * dvc=[[DetailViewController alloc]initWithTrick:str andOtherString:str1 andOtherString:str2];

        dvc.trickname=str;
        dvc.trickname2=str1;
        dvc.trickname3=str2;

        NSLog(@"%@",str);

        appdelegate.counter=0;

    [self.navigationController pushViewController:dvc animated:YES];

  }

然后转到Detailview Controller并通过Detailview控制器的init()方法访问这3个字符串。

首先在 DetailVewController.h

中定义init方法
-(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;

@property(nonatomic,retain)NSString * trickname;

@property(nonatomic,retain)NSString *  trickname2;

@property(nonatomic,retain)NSString *  trickname3;

<强> DetailViewController.m

@synthesize trickname;
@synthesize trickname2;
@synthesize trickname3;

-(id)initWithTrick:(NSString *)name_ andOtherString:(NSString *)name2_ andOtherString:(NSString *)name3_;

{

    self=[super init];
       if (self)
       {

           self.trickname=name_;
           self.trickname2=name2_;
           self.trickname3=name3_;

    }
    return self;
}