2013-06-23 35 views
1

我有什麼:關於@選擇和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]; 
} 
+0

嘗試在您的'ATableViewController'中添加'strong'屬性,該屬性將保存'CustomTabBarController'的一個實例,並將'addTarget:'位更改爲該屬性而不是'self' ...(創建實例後存儲在財產當然) –

回答

2

此行

[AAButton addTarget:self action:@selector(assisitantButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

把目標設置的self方法assisitantButtonPressed:,這是ATableViewController。它會拋出異常,因爲ATableViewController沒有該方法。正確的方法是將weak財產CustomTabBarControlleraddTarget添加到ATableViewController保持引用到的TabBar,而不是自我

@interface ATableViewController 

@property (weak, nonatomic) CustomTabBarController* tabBar; 
... 

@end 

@implementation ATableViewController 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tabBar = <you must somehow obtain the tab bar reference, either here or in init> 

    ... 

    [AAButton addTarget:self.tabBar action:@selector(assisitantButtonPressed:) forControlEvents:UIControlEventTouchUpInside];  

    ... 
} 

@end 

這是很好的,你想避免代碼重複。然而,由於ATableViewController,BTableViewController ......非常相似(從您的問題和他們的命名來判斷),您可能只想使用一個TableViewController但具有不同的實際數據。此外,如果該方法assisitantButtonPressed:真的屬於表視圖控制器,你把它搬到導航欄只是爲了避免重複代碼,它一個非常不好的做法。

+0

感謝您的回答!實際上TableViewControllers不是名字A,B,C,D,它們是如此不同。在這種情況下,將在三個TableViewControllers中使用相同的方法,所以我認爲重複它會更有效率? (更少內存),而不是在視圖加載時創建一次實例,而只是想避免重複。 – PlusA