我想在两个 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];
}
最佳答案
labelTtitle
是 GridViewController
的 GUI 元素,当您尝试在其他类中访问时它不会获得任何值,您最好尝试创建一个
并将值分配给另一个类中的此字符串,但是当您为 GridViewController
中的 NSStringGridViewController
加载 viewDidLoad
时然后像这样将此字符串分配给您的标签
-(void)viewDidLoad{
[super viewDidLoad];
self.labelTitle.text=self.stringYouCreated;
}
在 ViewController.m
中,将值分配给您在 GridViewcontroller
中创建的字符串属性,如下所示
grid.stringYouCreated = currow;
关于ios - 字符串赋值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15388244/