开发者

How to communicate an UIView from a XIB child to an UIView from a parent XIB?

I'd like to know if is possible to tap somewhere (or do something else) inside an UIView loaded from XIB file and trigger an action of an UIView from a parent XIB (like change the title of an UIButton in the parent UIView). This is a fragment of the code NewsController class interface:

#import <UIKit/UIKit.h>
#import "NewsPage.h"
@interface NewsController : UIViewController {
    // Some objects
    UIButton *loadButton;
    UIView *newsView;
    NewsPage *newsPage;
}
@property (nonatomic, retain) IBOutlet UIButton *loadButton;
@property (nonatomic, retain) IBOutlet UIView *newsView;
@property (nonatomic, retain) NewsPage *newsPage;
@end

and this is a fragment of the class implementation of NewsPage, where I load the child XIB:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Some code
    newsPage = [[NewsPage alloc] initWithNibName:@"NewsPage" bundle:nil];
    [newsView addSubview:newsPage.view];
    // Some other code
}

This is a fragment of code inside NewsPage class interface:

#import <UIKit/UIKit.h>
#import "NewsController.h"
@interface NewsPage : UIViewController {
    NewsController *newsController;
}
@property (nonatomic, retain) IBOutlet NewsController *newsController;
@end

and this is a fragment of the class implementation

- (void)tapAction:(UIGestureRecognizer *)gestureRecognizer {
    // Some code
    NewsController *newsController = [[NewsController alloc] initWithNibName:@"News" bundle:nil];
    NSLog(@"Current button title is: %@\n", newsController.loadButton.titleLabel.text);        
    newsController.loadButton.titleLabel.text = @"New text goes here!";
    NSLog(@"Current button title is: %@\n", newsController.loadButton.titleLabel.text);
    // Some other code
}

With this code (and after some wiring in IB) I see the correct title in debugger but the simulator doesn't update the title change. My guess is that the child UIViews are parallel to the parent's UIViews and even if I do something li开发者_JAVA技巧ke this:

[newsController.loadButton removeFromSuperview]

there is no change in the parents UIView hierarchy because the child UIViews "aren't inside" of parent UIView. I hope somebody can help with this because is very frustating. In advance, thank you very much.


One issue that I see is that you are accessing some of the elements in the NewsController that might not yet have been unpacked from the NIB since they are done lazily when the view is needed. Try to access the .view member of the controller, which should force the load, before you reference those items. This is a gotcha that everyone seems to hit until the lazy load idea is grokked.

NewsController *newsController = [[NewsController alloc] initWithNibName:@"News" bundle:nil];
newController.view; //hopefully this doens't get optimized out. If so, change it a bit.
NSLog(@"Current button title is: %@\n", newsController.loadButton.titleLabel.text);   


Essentially, when the parent wants to know something the Child will tell it, you set a delegate on the child as the parent, then in the child code you check to see if that delegate exists first of all, and then make sure it responds to the selector you will send. then you send it a message.

a good way to implement this in through the use of @protocol's

Here is a link with some basic information about it

http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜