我有什麼:關於@選擇和MVC
- 4 TableViewController(A,B,C,d) - 這將顯示四個內容 類別。
- 假的TabBarController基於UITabBarController,它 工作正常。
我想要做什麼:
添加上ATableView一個按鈕,它會用我FakeTabBarController的方法(因爲我想分享的方法,所以我可以用它在BTableViewController,CTableViewController而不是複製它兩三次)
所以我只是公開方法(在.h文件中),並將.h文件包含在我的ATableViewController中。然後, addTarget:action:forControlEvents:一如既往,但是..它不起作用,請幫忙!
錯誤:
[ATableViewController assisitantButtonPressed:]: unrecognized selector sent to instance 0x715a8d0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ATableViewController assisitantButtonPressed:]: unrecognized selector sent to instance 0x715a8d0'
*** First throw call stack:
(0x1ca1012 0x10dee7e 0x1d2c4bd 0x1c90bbc 0x1c9094e 0x10f2705 0x262c0 0x26258 0xe7021 0xe757f 0xe66e8 0x2ea1d3 0x1c69afe 0x1c69a3d 0x1c477c2 0x1c46f44 0x1c46e1b 0x1bfb7e3 0x1bfb668 0x22ffc 0x1c8d 0x1bb5)
libc++abi.dylib: terminate called throwing an exception
FakeTabBarController.h:
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UITabBarController
- (IBAction)assisitantButtonPressed:(UIButton *)sender;
@end
FakeTabBarController.m:
...
- (IBAction)assisitantButtonPressed:(UIButton *)sender
{
switch ([sender tag]) {
case 0: // AA
NSLog(@"AA");
break;
case 1: // BB
NSLog(@"BB");
break;
default:
break;
}
}
...
ATableViewController.m:
#import "ATableViewController.h"
#import "ATableCell.h"
#import "FakeTabBarController.h"
...
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *AAImage = [UIImage imageNamed:@"search.png"];
UIButton *AAButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.73, 0, AAImage.size.width, AAImage.size.height)];
[AAButton setTag:0];
[AAButton setImage:searchImage forState:UIControlStateNormal];
[AAButton addTarget:self action:@selector(assisitantButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:AAButton];
}
嘗試在您的'ATableViewController'中添加'strong'屬性,該屬性將保存'CustomTabBarController'的一個實例,並將'addTarget:'位更改爲該屬性而不是'self' ...(創建實例後存儲在財產當然) –