【发布时间】:2011-04-27 22:32:32
【问题描述】:
我想知道是否可以在从 XIB 文件加载的 UIView 内点击某处(或执行其他操作)并触发来自父 XIB 的 UIView 的操作(例如更改父项中 UIButton 的标题界面视图)。这是NewsController类接口的代码片段:
#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
这是 NewsPage 类实现的片段,我在其中加载子 XIB:
- (void)viewDidLoad {
[super viewDidLoad];
// Some code
newsPage = [[NewsPage alloc] initWithNibName:@"NewsPage" bundle:nil];
[newsView addSubview:newsPage.view];
// Some other code
}
这是 NewsPage 类接口中的一段代码:
#import <UIKit/UIKit.h>
#import "NewsController.h"
@interface NewsPage : UIViewController {
NewsController *newsController;
}
@property (nonatomic, retain) IBOutlet NewsController *newsController;
@end
这是类实现的一个片段
- (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
}
使用此代码(在 IB 中进行一些接线之后)我在调试器中看到了正确的标题,但模拟器没有更新标题更改。我的猜测是子 UIViews 与父 UIViews 平行,即使我做了这样的事情:
[newsController.loadButton removeFromSuperview]
父 UIView 层次结构没有变化,因为子 UIView“不在”父 UIView 的内部。我希望有人可以帮助解决这个问题,因为这非常令人沮丧。提前,非常感谢您。
【问题讨论】:
-
xib 是否正确连接到所有者对象?听起来 titleLabel 不存在于视图的层次结构中。所以不会显示。
-
@Toro:是的,所有者对象已正确连接,但我不知道父 UIView 中加载的 XIB 对象是否属于父对象的层次结构,如果是,为什么不更新从子 UIView 到父 UIView 所做的任何更改。
标签: iphone objective-c xcode ios interface-builder