简体   繁体   English

Xcode 4.2和Storyboard,如何从另一个类访问数据?

[英]Xcode 4.2 & Storyboard, how to access data from another class?

I was wondering how I could access data from another class using Xcode 4.2 and Storyboard? 我想知道如何使用Xcode 4.2和Storyboard访问另一个类的数据? Say for instance how would I access the text of a text field from another class? 例如说我将如何从另一个类访问文本字段的文本? Google hasn't helped and the lesson on MyCodeTeacher.com about this is outdated and doesn't work anymore... Google并没有提供帮助,在MyCodeTeacher.com上有关此问题的课程已过时并且不再起作用...

Thanks for bearing with me! 感谢您的支持!

-Shredder2794 -Shredder2794

Not sure if this is the only or best way, but you can create a property in the destination view's .h file and set it to a value before the segue is performed 不知道这是唯一还是最好的方法,但是可以在目标视图的.h文件中创建属性并将其设置为值,然后再执行segue

in the destination view controller's .h file: 在目标视图控制器的.h文件中:

@interface YourDestinationViewController : UIViewController
{
    NSString* _stringToDisplay;
    //...
}
    @property (nonatomic, retain) NSString* stringToDisplay;        
    //...

and in the presenting view's .m file 并在当前视图的.m文件中

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    YourDestinationViewController*viewController = segue.destinationViewController;
    viewController.delegate = self;
    viewController.stringToDisplay = @"this is the string";
}

Then you can do what you want with the property in whichever of the viewWillAppear/viewDidLoad/viewDidAppear/etc. 然后,您可以使用任何viewWillAppear / viewDidLoad / viewDidAppear / etc中的属性执行所需的操作。 methods best suits your purpose in the destination view's .m file 方法最适合您在目标视图的.m文件中的用途

And then to check if it works, in the destination view controller's .m file: 然后在目标视图控制器的.m文件中检查其是否有效:

-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"self.stringToDisplay = %@", self.stringToDisplay);
    ...
    //and if a label was defined as a property already you could set the 
    //label.text value here
}

Edit: Added more code, and made it less generic 编辑:添加了更多的代码,并减少了通用性

This isn't specific to Storyboard. 这不是故事板特有的。 There are several ways to do what you are trying to do. 有几种方法可以做您想做的事情。 You could declare a variable in your AppDelegate (an NSString) and set that in your first class. 您可以在AppDelegate(一个NSString)中声明一个变量,然后在第一个类中进行设置。 Then in your second class access the AppDelegate variable and use that to set your label. 然后,在第二个类中,访问AppDelegate变量,并使用该变量来设置标签。 The code to do this is: 执行此操作的代码是:

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
label.text = appDelegate.myString;

Another way to do it (probably the easiest) is to declare an NSString in your second class. 另一种方法(可能是最简单的方法)是在第二个类中声明一个NSString。 Then in your first class, before you push the second view set that string variable. 然后在您的第一堂课中,在您推送第二个视图之前,请设置该字符串变量。 Something like this: 像这样:

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"" bundle:nil];
vc.myString = @"";

The third way to do this is using delegates. 第三种方法是使用委托。 This is the most 'complicated' way but is the best. 这是最“复杂”的方法,但却是最好的方法。 You would create a delegate which gets called when your second view appears. 您将创建一个委托,该委托在第二个视图出现时被调用。 The delegate could then return the value from the first class to you. 然后,委托可以将值从第一类返回给您。

You may also be able to use the new completion handler block on the iOS 5 pushViewController: method. 您也许还可以在iOS 5 pushViewController:方法上使用新的完成处理程序块。

Edit: 编辑:

Custom init method: 自定义init方法:

- (void)initWithNibName:(NSString *)nibName bundle:(NSString *)bundle string:(NSString *)myString

And then when you are pushing the view just class this method and set the string through it. 然后,当您推送视图时,只需对该方法进行分类并通过它设置字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM