只有当我打开和关闭键盘时才会发生这种情况,这是“UIResponder”的问题吗?还是关于“removefromsubview”?
我有一个自定义工具栏,我的 UIBarButtonItems 似乎针对的是错误的 QuestionTableViewController 实例,因此它会触发堆栈中先前加载的 Controller 的选择器方法,而不是当前对象的选择器方法。
第一页显示正确,但当我第二次将此 Nib 加载到导航 Controller 时,它针对第一个对象的操作方法而不是当前对象。
在我的 RootViewController 中,我每次为不同的页面加载这个 nibs:
QuestionTableViewController *questionViewController = [[QuestionTableViewController alloc] initWithNibName:@"QuestionTableViewController" bundle:nil];
然后在 QuestionTableViewController 的 viewDidload 方法中执行此操作;
UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:nextArrow style:UIBarButtonItemStylePlain target:self action:@selector(localNextView:)] autorelease];
rightButton.width=120.0f;
self.customToolBar= [[[CustomToolbar alloc]
initWithFrame:CGRectMake(0,436,self.navigationController.view.frame.size.width, 44)] autorelease];
[self.customToolBar setItems:[NSArray arrayWithObjects:bckButton, rightButton, nil] animated:NO];
[self.navigationController.view addSubview:self.customToolBar];
-(void) localNextView:(id)sender {
//i.e when i am on the third page, here when i check the
//sender I see the object belongs to first page!!
}
然后我在 viewWillDisappear 上删除它,我从 navigationcontroller 中删除它以确保下一个 View 使用它自己的工具栏而不是以前的实例;
[self.customToolBar removeFromSuperview];
self.customToolBar=nil;
最佳答案
您可以在每次显示 View 时保持相同的 uibarbutton 和选择器目标。不过,您应该更改触发方法的内容。您可以在 ViewWillAppear 中执行此操作,因为 ViewDidLoad 仅被调用一次。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// do de stuff for this view-show here.
// could be something like this.
// viewcounter is a property and
// an integer set to 0 in the ViewDidLoad
// and updated with viewcounter = counterview+1 each time you go to next page
switch (viewcounter){
case 1: { do A ; break;}
case 2: { do B ; break;}
}
}
你可以做的是改变标签的文本,设置值或按钮的标题等等。当然这些项目必须是导出
关于iPhoneSDK : UIBarButtonItem's selector method targets wrong instances,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8801641/