ios - 字符串赋值不起作用

标签 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];

 }

最佳答案

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

-(void)viewDidLoad{

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

}

ViewController.m 中,将值分配给您在 GridViewcontroller 中创建的字符串属性,如下所示

   grid.stringYouCreated = currow;

关于ios - 字符串赋值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15388244/

相关文章:

iOS 获取最后一帧视频

ios - UIScrollView:ViewController View 框架更改后滚动内容

ios - 如何在自定义容器 View Controller 中实现交互式转换

ios - boundingRectWithSize 忽略换行符

ios - 如何将整个句子中的某个单词定义为变量?

ios - __strong 和 strong,__weak 和 weak 之间有什么区别?

ios - 约束使宽度等于另一个

ios - 如何在一个 UIViewController 中处理多个 View

objective-c - 如何循环遍历 NSString 中的各个 NSString? (语法突出显示/着色)

nsstring - stringWithContentsOfFile 已弃用