gpt4 book ai didi

ios - 自定义按钮出错

转载 作者:行者123 更新时间:2023-11-28 22:29:08 25 4
gpt4 key购买 nike

我有一个错误:

2013-08-01 01:09:18.433 VstupHelper[28332:11303] -[VHMasterViewController popViewController:]: unrecognized selector sent to instance 0x7566410
2013-08-01 01:09:18.434 VstupHelper[28332:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[VHMasterViewController popViewController:]:

这是我的代码:

UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 50, 30)];
[bt setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
[bt addTarget:self action:@selector(popViewController:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:bt];
self.navigationItem.leftBarButtonItem=leftButton;

self.navigationItem.leftBarButtonItem.target = self.revealViewController;
self.navigationItem.leftBarButtonItem.action = @selector(revealToggle:);

我的英语不好,所以如果我难以理解,我深表歉意。

最佳答案

你有几个问题:

1) 你正在调用 popViewController: 我假设是一个 UIViewController 子类,而你应该在你的 UINavigationController 上调用它。

2) popViewController: 采用 bool 值来确定是否为该过渡设置动画。通过该操作,默认情况下它会传递给发件人。

3) 然后为按钮重新分配目标和操作(但是当您使用 initWithCustomView: 初始化程序创建 UIBarButtonItem 时不会调用这些:See docs) .


如何解决这个问题:

1) 调用您为按钮处理程序创建的方法:

UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 50, 30)];
[bt setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
[bt addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:bt];
self.navigationItem.leftBarButtonItem=leftButton;

2) 在那个方法中,弹出 View Controller ,或者做任何你真正想做的事:

- (void)buttonTouched:(id)sender 
{
[self.navigationController popViewController:YES];
//Or what you assigned to the button action:
[self.revealViewController revealToggle:(not sure what you're supposed to have here)];
}

关于ios - 自定义按钮出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com