开发者

Reloading A View iPhone

开发者 https://www.devze.com 2023-03-31 05:25 出处:网络
So I have two views A and B. A is a profile view, B is a login view. A loads B in the ViewDidLoad Method using

So I have two views A and B. A is a profile view, B is a login view. A loads B in the ViewDidLoad Method using

LoginViewController *lvc = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]]; //make new instance of LoginViewController
        [self presentModalViewController:lvc animated:NO]; //present the LoginViewController
        [lvc release];

in the login View, if the login is successful, the view is removed

[self dismissModalViewControllerAnimated:YES];

On the login view, It downloads some data which I want to display on the profile view. How would I go about sending the data to the profile view 开发者_如何学Cand displaying it in the xib. I believe the profile view is already displayed but is just hidden.


This is a basic "communicate between two classes" question. There are many ways to do this, but here are three. I only wrote sample code for delegation (because I think that's probably the best choice in your situation), but let me know if you want examples of notifications or KVO.

Delegation Implement a delegation or callback method in Class A. Delegation is best for small class hierarchies. If Class A is the only class that will load B and A is the only class who cares what happens in B, then delegation is the easiest way to move data around. It's simple to implement, it's simple to understand and there's a clear relationship between the classes.

// Class A
- (void)displayLoginViewController {
    LoginViewController *lvc = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
    lvc.delegate = self;
    [self presentModalViewController:lvc animated:NO]; //present the LoginViewController
    [lvc release];
}

- (void)loginViewControllerWasSuccessfull:(LoginViewController *)loginViewController {
    // Do whatever you need to do here
    [self dismissModalViewControllerAnimated:YES];

}

In the login view controller do something like this in the header:

@property (assign) NSObject delegate; // declared assign so you don't have circular references

… and this in the implementation:

- (void)didLogin {
    [self.delegate loginViewControllerWasSuccessfull:self];
}

Notification Class A will register to listen for login notifications. Class B will post login notifications. Notifications are best if the classes that care about login are distributed. i.e. there are many classes that care about a login event and they may not necessarily have a direct relationship with the class that is performing the login.

Key Value Observing KVO is best if you don't particularly care about the login event, you care about the changes to the data. You will have some class that manages your data, probably an NSManagedObject if you are using Core Data. Class A will observe changes to whatever property it's interested in. Your LoginViewController will update that data class when it is finished downloading data. Class A will be notified that the data has changed.

Whatever solution you decide to use, the choice ultimately comes down to asking, "What does Class A care about?". Does Class A need to know that Class B successfully logged in? Use delegation. Does Class A need to know that somewhere, some class logged in? Use notifications. Does Class A not care about logins, it only needs to know if data has changed? Use KVO.


You Load view A after downloading the data instead of ViewDidLoad.


when u click on the login button then download data and display it. if your viewWillAppear is not calling then create nsnotification center object and post it when you want to call your view willAppear method.and then remove this notification.


you can store the downloaded data at delegate file in login view. And in viewWillAppear method of profile view use data from the delegate....


for that you have to create variable and set its property in .h and .m file .than you can set this variable value in login screen and it will synthesize to profile screen.

Another way you have to create variable in appDalegate . appDalegate value set in login screen and use this value in profile screen


If I understand correctly, you are trying to do the equivalent of Android's Intents. Therefore I advise using iOS's NSNotificationCenter and send NSNotifications with associated data.

0

精彩评论

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