开发者

UITableview add data from another class

Ok so I have a UITableView and a UINavigationController with a UIBarButtonSystemItemAdd and when the add button is pushed, it takes the us开发者_开发知识库er to a new view with some fields. I want to be able to get the information the user puts in these fields and display it in the table view. I want the user to be able to add as many of those items as they want.

Now that I think of it, kind of like the contacts app. The user fills out a contact and then when they go back to the main list, the new contact is there.

What is the best way to do this. I tried NSUserDefaults and a bunch of arrays, but that got really messy. Thanks.


A few ways I can think of handling this. The easiest and best way would be to use a model presentation of your view controller instead of pushing to it, much like the contact app on the iPhone does. That way you can access all your data needed with your tableview view controller instead of trying to pass it back and forth. once the user is finished with the creation view controller the model view is dismiss and you can add from your tableview. Developer doc: http://developer.apple.com/library/ios/ipad/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html%23//apple_ref/doc/uid/TP40007457-CH112-SW16


Did you use NSUserDefaults properly ? Use like this.

In one class:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]
[prefs setInteger:1 forKey:@"integerKey"];

In another class:

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 NSInteger myInt = [prefs integerForKey:@"integerKey"];

//Now myInt has the value 1.

You could also have NSNotification and pass an NSDictionary type:

NSDictionary *info = [NSDictionary dictionaryWithObject:@"Brad Pitt" forKey:@"User Name"];


[[NSNotificationCenter defaultCenter] postNotificationName:@"UserNameNotification" object:self userInfo:info];

And then you could add observers and write the method for the corresponding Selector.


The best way is model presentation style of UIController. when user dismiss the view your table view get notified by delegate method and there you can add that data in table view.

Now if you do not want to use model view style then You can use NSUserDefault.

 Thats what I was trying to do, but the user could ultimatly enter in hunderds of data.

You do not have to think about it what u should do is that you first reset the NSUserDefault. And then save all the data in NSUserDefault and then get that data from table view.

[NSUserDefaults resetStandardUserDefaults];

One thing more u can do is that make a variable in Delegate and save data in that and get it in table view. but i do not recommend you to do this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜