字符串的分配不起作用

时间:2013-03-13 14:33:35

标签: ios uiviewcontroller nsstring

我想在两个viewcontroller之间传递一个字符串,但总是(null)

我的代码

GridViewController.h

 #import <UIKit/UIKit.h>

 @interface GridViewController : UIViewController {

 }

 @property (nonatomic, strong)UILabel * labelTitle;

 @end

ViewController.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];

 }

3 个答案:

答案 0 :(得分:3)

在您为其分配文本时,不存在labelTitle的现有实例。 Label从xib实例化,然后才能保存该值。

答案 1 :(得分:3)

labelTtitleGridViewController的GUI元素,当您尝试在其他类中访问时,它不会获得任何值。您最好尝试在{{1}中创建NSString 1}}并将值分配给另一个类中的此字符串,但是当您为GridViewController加载viewDidLoad时,请将此字符串指定给您的标签,如下所示

GridViewController

-(void)viewDidLoad{ [super viewDidLoad]; self.labelTitle.text=self.stringYouCreated; } 中为您在ViewController.m中创建的字符串属性赋值

GridViewcontroller

答案 2 :(得分:1)

你的labelTitle似乎是零。它应该是使用viewDidLoad

GridViewController方法创建的