0

我想在两个视图控制器之间传递一个字符串,但总是(空)

我的代码

GridViewController.h

 #import <UIKit/UIKit.h>

 @interface GridViewController : UIViewController {

 }

 @property (nonatomic, strong)UILabel * labelTitle;

 @end

视图控制器.m

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

       NSString *currow =[self.mutableArray objectAtIndex:indexPath.row];
       NSLog(@"the string is %@", currow);
       //the string is Grappe, Brandy and Cognac

       GridViewController * grid = [[GridViewController alloc]initWithNibName:@"GridViewController~iPhone" bundle:nil];

       grid.labelTitle.text = currow;

       NSLog(@"the string is %@", grid.labelTitle.text);
       //the string is (null)

       NSLog(@"the string is  %@", currow);
       //the string is Grappe, Brandy and Cognac

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

 }
4

3 回答 3

3

在您将文本分配给它时,不存在 labelTitle 实例。Label 从 xib 实例化,然后它才能保存该值。

于 2013-03-13T14:37:36.870 回答
3

labelTtitle是 的 GUI 元素,GridViewController当您尝试在其他类中访问时它不会获得任何值,您最好尝试NSString 在您的类中创建一个GridViewController并将值分配给其他类中的此字符串,但是当您加载您viewDidLoadGridViewController时像这样将此字符串分配给您的标签

-(void)viewDidLoad{

    [super viewDidLoad]; 
    self.labelTitle.text=self.stringYouCreated;

}

ViewController.m为您创建的字符串属性赋值时,GridViewcontroller像这样

   grid.stringYouCreated = currow;
于 2013-03-13T14:37:41.200 回答
1

labelTitle似乎是零。它应该是用以下viewDidLoad方法创建的GridViewController

于 2013-03-13T14:39:31.843 回答