【问题标题】:Passing values from one ViewController to another in Objective-C在 Objective-C 中将值从一个 ViewController 传递到另一个 ViewController
【发布时间】:2015-11-02 19:35:20
【问题描述】:

我正在尝试将 NSString (message) 从 ViewController(发件人)传递给 MyViewController(收件人)。

我在ViewController 中创建了一个名为testViewContollerMyViewController 实例,我通过它使用setTitle: 方法发送NSString

MyViewController *testViewController = [[MyViewController alloc] init];
[testViewController setTitle:message];

这里是MyViewController.h

- (void)setTitle:(NSString*)title;

这里是MyViewController.m

- (void)setTitle:(NSString*)title {

_testField.text = title;

}

我不完全确定为什么这不起作用,但我认为这与 viewDidLoad 在调用 setTitle: 之前加载有关。

您能提供的任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 您必须使用情节提要实例化视图控制器
  • 不知道不工作是什么意思。在 setTitle 方法中放置标题的 NSLog。
  • 查找'prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender'
  • 这种方法的问题是,当你初始化方法并调用方法“setTitle”时,_testField.text 是一个 UIKit 属性,直到 viewDidLoad 方法被执行后才被初始化和设置。您必须使用属性来设置标题,然后在 viewDidLoad 方法中使用该属性,如下所示 _textField.text = self.title;

标签: ios objective-c uiviewcontroller


【解决方案1】:

_testField 是在哪里创建的?如果它在viewDidLoad 中,那么当你调用setTitle 时它就是nil,这意味着它是一个空操作。 viewDidLoad 在视图添加到窗口时被调用,这意味着如果你只是在视图控制器上调用 init viewDidLoad 可能永远不会被调用。

我会在MyViewController 上定义一个属性,即所需的字段文本,然后在viewDidLoad 中添加_testField.text = title

MyViewController *testViewController = [[MyViewController alloc] init];
testViewController.testFieldText = message;

- (void)viewDidLoad {
     [super viewDidLoad];

     // do something to initialize _testField here

     _testField.text = title;
}

还要注意UIViewController 已经有一个名为title 的属性,并且覆盖setTitle: 可能会产生您没有预料到的副作用。

【讨论】:

    【解决方案2】:

    这基本上和内存分配给_testField的时候有关。 如果您以编程方式在viewDidLoad 中创建_testField

    MyViewController *testViewController = [[MyViewController alloc] init];
    testViewController.testFieldText = message;
    

    这两行没有帮助,因为内存还没有分配给_testField so the text cannot come up.即使你在故事板上创建它并在方法中分配字符串

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
        {
    
                SecondViewController *obj = segue.destinationViewController;
                [obj setTitle:@"New Title"];
        }
    

    这也无济于事,因为内存再次未分配。这个问题的简单解决方案是:

    1. 在设置_testField 之前访问MyViewController 的视图属性,例如:

      MyViewController    *obj = [[MyViewController alloc] init];
      [self.navigationController pushViewController:obj animated:YES];
      [obj setTitle:@"New Title"];
      
    2. MyViewController 中设置@property 中的字符串,并将字符串从属性重新分配给_testField

    是的,setTitleUIViewController 的一个属性,如果你自定义它会弄乱默认属性。安全起见... :)

    【讨论】:

    • 通过使用 NSLogs 我能够注意到 ViewDidLoad 发生在 setTitle 之前,但值仅在 setTitle 发生之后显示。
    • 如果您只添加任何与视图相关的调用,那么它将首先被调用,我们需要将字符串保存在新视图控制器的内存位置中。
    【解决方案3】:

    在第一个 ViewController 中,您可以使用以下内容

    SecondViewcontroller *MyViewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"SecondViewcontroller"];
    MyViewController.Title = @"Welcome";
    [self presentViewController:MyViewController animated:YES completion:Nil];
    

    在 secondViewcontroller 中使用以下

    secondViewcontroller.h 文件编辑

    @interface SecondViewcontroller : UIViewController
        @property (strong, nonatomic) NSString *Title;
    @end
    

    然后将 Title 值打印到 secondviewcontroller

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多