0

我无法在objective-C(Xcode)中将值从一个viewController传递到下一个。下一个 viewController 中的变量值为 null。我不知道我在这里错过了什么,我完全被困住了。请帮帮我。

这是我的代码第一个 viewController(detailsViewController.h):

    @property (weak, nonatomic) IBOutlet UITextField *fromTextField;

.m

ResultViewController *resultObject=[self.storyboard instantiateViewControllerWithIdentifier:@"ResultViewController"];
resultObject.fromValueFromDVC=self.fromTextField.text;
[self presentViewController:resultObject animated:YES completion:nil];

第二个视图控制器(ResultViewController.h)

    @interface ResultViewController : UIViewController<NSXMLParserDelegate>
    @property (weak, nonatomic) IBOutlet UILabel *fromLabel;
    @property(strong,nonatomic) NSString *fromValueFromDVC;

.m

    - (void)viewDidLoad
    {

   [super viewDidLoad];        
    self.fromLabel.text=self.fromValueFromDVC;
    }

请帮忙。提前非常感谢!

4

1 回答 1

2

在您的 prepareForSegue 方法中使用segue.destinationViewController。而不是 [self.storyboard instantiateViewControllerWithIdentifier:@"ResultViewController"];

(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{



 if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
   {
    ResultViewController *resultObject= segue.destinationViewController
    resultObject.fromValueFromDVC=self.fromTextField.text;

   // If you have the segue rightly set up you don't need this like here.
    **[self presentViewController:resultObject animated:YES completion:nil];**
   }

  }

希望能帮助到你。

于 2013-06-04T18:03:40.880 回答