0

我正在开发一个应用程序,其中有三个不同的表和一个包含标签的 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

4 回答 4

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;
}

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

于 2013-05-04T07:26:37.377 回答
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 方法中访问它们。

希望它可以帮助你。

于 2013-05-04T05:50:40.917 回答
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 文件中详细声明视图控制器

于 2013-05-04T06:04:55.917 回答
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 控制器并通过 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;

细节视图控制器.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;
}
于 2013-05-06T05:32:26.220 回答