开发者

Objective-C (iOS) Access UITextField's text from another class

this question is extremely basic and I apologize if the answer should be staring me in the face.

So: I have a UITableView and another viewController. I'm updating some information for CoreData in the UITableView's .m file. In order to do this I need开发者_开发知识库 the text in a text field in the other viewController.

So being rather inexperienced, I tried:

[entity setName:[NSString stringWithFormat:@"%@", viewController.entityNameInputField.text]];

I've renamed a few things for clarity. Entity is my entity (no kidding), the attribute I'm setting is called Name, viewController is where the textField is and the text field is called entityNameInputField.

I'm a lot more interested in why this sort of thing doesn't work than what the solution to my problem might be. Why can't I just get the text from the text field in this way?

I suppose to word everything differently:

If I'm in Class A and I want to access the value of say, an NSString in class B, why can't I just use ClassA.TheString or [ClassA TheString] .... from what I've managed to understand, I'm sending these objects getter methods and I can't seem to see why this doesn't work...

Any help would be appreciated :)


Some more code would be useful to see what might be causing your confusion, but if you have declared properties in your classes the associated instance variables (or have manually declared accessors) then you will be able to access the associated ivars.

So, something like this (below) will work fine.

MyClassA * instanceOfA = [[myClassA alloc] init];
MyClassB * instanceOfB = [[myClassB alloc] init];

instanceOfA.myProperty = instanceOfB.myProperty;


As far as I understand, this is not a problem of accessing values from another class. Assuming that both view controllers are retained in memory, you can always call each other's getters/setters.

I assume that you've got a UITableViewController named A and then you're presenting a custom UIViewController named B with a text field. The user taps something in the text field and you want this text to go back to your table view controller. If inside B you have a valid reference to A, you can then call it's setter for the appropriate variable, and then dismiss the custom controller.

For example, in B's header you could have:

@interface MyViewController : UIViewController {
     id tableViewController;
}
@property (nonatomic, retain) id tableViewController;

then in A, when you're creating B:

MyViewController *B = [[MyViewController alloc] init];
B.tableViewController = self; // this passes a reference to A

so when you're in B and the user has entered input in the text field, you could do something like

[self.tableViewController setVarname:textField.text];

assuming there's a varname variable in A, and a textField in B.

You can also gain access to A without having to create a separate variable, by calling

[self parentViewController]

if that suits you better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜