2011-06-17 43 views
1

我已經創建了子類UIToolbar,以便更輕鬆地實現下一步的Safari瀏覽器,之前完成了某些操作。 這一切工作正常,當我直接添加它(即不是子類),但現在我是,它崩潰每次我點擊其中一個按鈕與 -[keyboardToolBar hideKeyboard:]: unrecognized selector sent to instance 這是我第一次嘗試子類的東西,所以我沒有當然,如果我做錯了一些事情。子類化時崩潰UIToolbar

代碼的子類

@interface keyboardToolBar : UIToolbar { 
UIToolbar *keyboard; 
UIBarItem *previous; 
UIBarItem *next; 
UIBarItem *done; 
} 

@property (nonatomic, retain) UIToolbar *keyboard; 
@property (nonatomic, retain) UIBarItem *previous; 
@property (nonatomic, retain) UIBarItem *next; 
@property (nonatomic, retain) UIBarItem *done; 

-(void)previousField; 
-(void)nextField; 
-(void)hideKeyboard; 


@end 


#import "keyboardToolBar.h" 


@implementation keyboardToolBar 
@synthesize keyboard, previous, done, next; 

-(UIToolbar*)initWithFrame:(CGRect)frame{ 
//Create a new toolbar 
keyboard = [[UIToolbar alloc]initWithFrame:frame]; 

//Create all the buttons and point them to methods 
UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" 
                  style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(previousField:)]; 
UIBarButtonItem *nextButton  = [[UIBarButtonItem alloc] initWithTitle:@"Next" 
                  style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(nextField:)]; 
UIBarButtonItem *filler  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                  target:nil 
                  action:nil]; 
UIBarButtonItem *doneButton  = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                  style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(hideKeyboard:)]; 

//Set the width of both of the buttons to make it look pritty 
previousButton.width = 70.0f; 
nextButton.width = 70.0f; 

self.previous = previousButton; 
self.next = nextButton; 
self.done = doneButton; 

//Add the buttons to the toolbar 
[keyboard setItems:[[[NSArray alloc] initWithObjects:self.previous, self.next, filler, self.done, nil] autorelease]]; 


//Release the buttons 
[previous release]; 
[next release]; 
[filler release]; 
[done release]; 


//return the shiny new toolbar 
return keyboard; 
} 

-(void)previousField{ 

} 

-(void)nextField{ 

} 

-(void)hideKeyboard{ 
NSLog(@"hello"); 
} 


@end 

並使用UIToolbar *keyboard = [[keyboardToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];

我已經試過所有我能想到的,但仍然得到同樣的錯誤調用。我敢肯定,我只是不保留一些地方指向按鈕來錯了地方,但任何幫助將長久地感謝 感謝 斯達克

回答

0

你(或你正在做的事情)是調用hideKeyboard:(注意冒號,表明該方法有一個參數。)。但是,您已實施的hideKeyboard方法不包含任何參數。

最有可能的,hideKeyboard應改爲:

- (void)hideKeyboard:(id)sender { 
    NSLog(@"hello"); 
} 

順便說一句,對於類名的一貫作風是大寫的,所以你的子類應該是KeyboardToolBar(我也建議你保持相同的駱駝作爲蘋果的課,所以KeyboardToolbar將是最好的)。

+0

大壩吧,新的它會很簡單。謝謝 – Darc

1

這裏的問題是,你的按鈕調用該採取輸入功能,但你的函數不採取輸入:

UIBarButtonItem *doneButton  = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                  style:UIBarButtonItemStyleBordered 
                  target:self 
                  action:@selector(hideKeyboard:)]; 

這意味着,必須有一個方法hideKeyboard:(id)sender。但是,你必須

-(void)hideKeyboard{ 
NSLog(@"hello"); 
} 

不管是輸入從選擇呼叫添加到函數或刪除:

1

您有效地寫了它作爲一個類的方法,這裏是一個更好一點的版本,因爲內存管理問題等

- (UIToolbar *)initWithFrame:方法(的CGRect)框架{ //創建一個新的工具欄

if ((self = [super initWithFrame:frame])) 


//Create all the buttons and point them to methods 
previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(previousField:)]; 

next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextField:)]; 
UIBarButtonItem *filler  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
done  = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(hideKeyboard:)]; 

//Set the width of both of the buttons to make it look pritty 
previous.width = 70.0f; 
next.width = 70.0f; 

//Add the buttons to the toolbar 
[keyboard setItems:[[[NSArray alloc] initWithObjects:previous, next, filler, done, nil] autorelease]]; 


//Release the buttons 
[previous release]; 
[next release]; 
[filler release]; 
[done release]; 


//return the shiny new toolbar 
return self; 
} 

也解決你的方法是要麼 @selector(someAction)匹配-(void)someAction;@selector(someAction:)匹配-(void)someAction:(id)sender;

也沒有理由保留對UIToolBar *鍵盤的引用,因爲你希望這是自我。 實際上可能沒有理由繼續引用按鈕,除非您稍後需要更改標題或動作/目標對。

+2

不完全:原始代碼返回一個UIToolbar而不是keyboardToolBar(幾乎肯定是錯誤的)並泄漏正在接收efents的keyboardToolBar! –

+0

是的,你是對的。我甚至沒有注意到這一點。 –