开发者

How to delegate back to the previous veiwcontroller

开发者 https://www.devze.com 2023-02-24 07:17 出处:网络
here is my navigation on my app 1) homescreenview co开发者_如何学Cntroller-->composemessageviewcontroller (i am able to use delegate to send data back to homescreenview)

here is my navigation on my app

1) homescreenview co开发者_如何学Cntroller-->composemessageviewcontroller (i am able to use delegate to send data back to homescreenview)

2)homescreenview controller -->messageslistcontroller(tableview)-->detailmessageviewcontroller(which is where my reply button is).

my problem is when i hit reply i want to send back information to homescreenviewcontroller with delegate . how can i do this?

Thanks in advance.

----UPDATE

@XJones, thanks for the detailed explanaion. is this what is should be doing in when i push detailview? please correct me if i am wrong.

  • (void)pushDetailMessageController{ DetailMessagetController *detailmessage = [[DetailMessagetController alloc] init]; detailmessage.delegate = self; // push messageListController onto navigation controller here [detail release]; }


that's a very general question. you're basically asking how to pass information from one controller to another controller. There are different ways to do that, a protocol (what a delegate generally communicates through) is one of them. The quickest thing you can do w/o making assumptions in your code that may be problematic later would be to pass the homeScreenController along as you push messageListController and then detailMessageController. You'll need to define an iVar and property in messageListController and detailMessageController to do this.

Something like:

in messageListController.h:

#import "HomeScreenController.h"

@interface messageListController : UITableViewController {
    // your iVars
    HomeScreenController *homeScreenController;
@end

@property (nonatomic, assign)    HomeScreenController *    homeScreenController;

add the same iVar and property for homeScreenController to detailMessageController.

in homeScreenController.h:

- (void)pushMessageListController
{
    MessageListController *messageListController = [[MessageListController alloc] init];
    messageListController.homeScreenController = self;
    // push messageListController onto navigation controller here
    [messageListController release];
}

in messageListController do the same thing as above when creating and pushing detailMessageController. Now, in detailMessageController you can send messages directly to homeScreenController.

If you want to generalize the above implementation so your controllers aren't specifically knowledgeable about each other then you can define a protocol and pass homeScreenController through as a delegate supporting that protocol.


How about adding a method in the messageslistcontroller? I personally would add the delegate "homescreenview" to the detailmessageviewcontroller as the messageslistcontroller doesn't have anything to do with the reply and apperantly the homescreenviewcontroller does.

when you create the detailmessageviewcontroller in the messageslistcontroller do this:

detailmessageviewcontroller.homeScreenDelegate = self.delegate;


One approach (without delegation)

as you are using navigationController, so [[self.navigationController viewControllers]objectAtIndex:0] will always return you homeScreenViewController.....you can use this object....

Thanks,

0

精彩评论

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