iPhoneSDK : UIBarButtonItem's selector method targets wrong instances

标签 iphone ios ios4

只有当我打开和关闭键盘时才会发生这种情况,这是“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/

相关文章:

ios - 为什么我无法在 iOS 11 中设置搜索栏的文本颜色?

ios - 如何通过 NSDate 加载带有今天日期的文本字段?

ios - iPhone 6s及更高版本设备中的UIPickerView宽度问题

iphone - 在 iOS4 中,applicationWillEnterForeground 可能会在 applicationDidEnterBackground 之前被调用,这会产生问题

ios - 应用评论多久会出现在 iTunes Connect 中?

ios - 当我尝试在 App Store 上上传新版本时,applink 出现 iTunes 错误 90046

iphone - iOS SDK 4.0 中 UIImageWriteToSavedPhotosAlbum 速度慢吗?

iphone - 在 XCode 4.6 下针对 iOS 4.x 设备

ios - 像around me应用程序一样的动画和加载

ios - 如何在 UITableView 中显示数组中的 x 个对象,然后在向下滚动时显示更多对象?