【问题标题】:How can i pass the data from one vc to another vc using push method (without segue) - Closed我如何使用推送方法(无 segue)将数据从一个 vc 传递到另一个 vc - 已关闭
【发布时间】:2014-10-23 06:03:51
【问题描述】:

我遇到以下问题:

有两个视图控制器,viewController1 和 viewController2。

在 viewController1 中,UIButtonUITextFieldviewDidLoad 中以编程方式创建,并且按钮在按下时会查看 viewController2。

viewController2 出现时有没有办法从 viewController1 中的UITextField 获取数据。

因为我是编程新手,所以我会适当地给出一个详细解释的答案。

谢谢你..

【问题讨论】:

标签: ios objective-c


【解决方案1】:

像这样在你的 VC2 中创建一个属性:

@property(nonatomic,strong) NSString* yourData;

在 VC1 中的按钮按下事件中,执行以下操作:

ViewController2 *viewController2 = [[ViewController2 alloc] initWithNib:@"ViewController2" bundle:nil];
viewControllerB.yourData = myTextfield.text;
[self pushViewController: viewController2 animated:YES];

希望这会有所帮助... :)

【讨论】:

    【解决方案2】:

    实际上,您的问题有很多解决方案。

    1. 使用 segues 传递数据。

    2. 使用应用委托文件中声明的属性传递数据。

    现在,这两种东西都有不同的用途,具体取决于您的需要。你应该详细学习这些东西。在这里解释它们不是一个好主意。

    http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ http://www.cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

    这些链接应该可以帮助您理解。

    解决您的问题:

    在 AppDelegate.h(应用程序委托文件)文件中,声明一个属性。

    @property NSString *name;
    

    在 AppDelegate.m(应用程序委托文件)中合成它。

    @synthesize name;
    

    现在,在 ViewController1.h 中声明 AppDelegate 对象。

    AppDelegate *delegate;
    

    在 ViewController1.m 的 viewDidLoad 中输入:

    delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    

    现在,在 ViewController 的按钮按下事件中,添加这个。

    //get data from textField and assign it to delegate.name
    //delegate.name = textField.text;
    

    在 viewController2 中创建一个委托对象并访问该值。

    //Declare a delegate object in viewController2 and create the delegate object in viewDidLoad of viewController2
    
    //Use delegate.name
    //NSLog(@"%@",delegate.name);
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 2020-10-02
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多