次要代表的问题
我有两个 UITableView,每个视图在同一个 UIViewController 中都有不同的委托。
我试图在另一个委托中调用控件,但失败了。
简介:
mainViewController 包含 UITableView(带有 UITableViewDelegate 和 UITableViewDataSource)+ UIImageView。
secondaryTable 包含 UITableView(带有 UITableViewDelegate 和 UITableViewDataSource)。
我想从 secondaryTable 调用 UIImageView。
In .h
@interface secondaryTable : UIViewController <UITableViewDataSource,UITableViewDelegate>
@end
@interface mainViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {
@property (nonatomic,retain) IBOutlet UIImageView *navbar;
@property (nonatomic,retain) IBOutlet UITableView *Table1;
@property (nonatomic,retain) IBOutlet UITableView *Table2;
@end
In .m
@implementation secondaryTable
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (indexPath.row == 0) cell.textLabel.text =@"A";
if (indexPath.row == 1) cell.textLabel.text =@"B";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected");
mainViewController *myController = [[mainViewController alloc] init];
[myController.navbar setHidden:NO];
}
@end
@implementation mainViewController
- (void)viewDidLoad {
Table1.delegate = self;
Table1.dataSource = self;
secondaryTable *myDelegate = [secondaryTable alloc];
Table2.delegate = myDelegate;
Table2.dataSource = myDelegate;
@end
}
在控制台日志中键入“Selected”,但不会显示控件。
为什么不能调用mainViewController中的navbar控件?
I have two UITableView, each one has a different delegate in the same UIViewController.
I'm trying to call control in another delegate, but it failed.
Brief:
mainViewController contains UITableView (with UITableViewDelegate and UITableViewDataSource) + UIImageView.
secondaryTable contains UITableView (with UITableViewDelegate and UITableViewDataSource).
I want to call UIImageView from secondaryTable.
In .h
@interface secondaryTable : UIViewController <UITableViewDataSource,UITableViewDelegate>
@end
@interface mainViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {
@property (nonatomic,retain) IBOutlet UIImageView *navbar;
@property (nonatomic,retain) IBOutlet UITableView *Table1;
@property (nonatomic,retain) IBOutlet UITableView *Table2;
@end
In .m
@implementation secondaryTable
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
if (indexPath.row == 0) cell.textLabel.text =@"A";
if (indexPath.row == 1) cell.textLabel.text =@"B";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Selected");
mainViewController *myController = [[mainViewController alloc] init];
[myController.navbar setHidden:NO];
}
@end
@implementation mainViewController
- (void)viewDidLoad {
Table1.delegate = self;
Table1.dataSource = self;
secondaryTable *myDelegate = [secondaryTable alloc];
Table2.delegate = myDelegate;
Table2.dataSource = myDelegate;
@end
}
It types "Selected" in console log, but it won't show control.
Why can't it call navbar control in mainViewController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议通过让两个表指向同一个委托来做到这一点,但在委托方法中,根据它所在的表选择行为。例子:
I would suggest doing this by having both tables point to the same delegate but, within your delegate methods, select behaviour based on which table it is. Example: