0
在我的測試項目中,我計劃擁有多個選擇器視圖。對於這些選擇器視圖,將會有UIToolbars和自定義的UIBarButtons。我試圖創建一個類方法,這樣我就不必重複代碼,但它看起來像按鈕沒有顯示出來。UIToolbar自定義UIBarButtonItem
機械手完成Button.h
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (test)
+ (UIBarButtonItem*)barButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector;
@end
機械手完成Button.m
#import "PickerDoneButton.h"
@implementation UIBarButtonItem (test)
+ (UIBarButtonItem*)barButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tintColor = color;
button.backgroundColor = [UIColor yellowColor];
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithCustomView:button];
return doneButton;
}
@end
而且在我查看Controller.m或者使用此類方法(我省略選擇器視圖方法後來viewDidLoad)
#import "ViewController.h"
#import "PickerDoneButton.h"
@interface ViewController()
@property (strong, nonatomic) NSArray *numberofPeople;
@property (strong, nonatomic) UIPickerView *countPeople;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.countPeople = [[UIPickerView alloc] init];
self.countPeople.dataSource = self;
self.countPeople.delegate = self;
self.numberofPeople = @[@"1",@"2",@"3"];
UIToolbar *countPeopleToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,0,50)];
countPeopleToolbar.barStyle = UIBarStyleBlackOpaque;
// Call the UIBarButtonItem from PickerDoneButton.h
countPeopleToolbar.items = @[[UIBarButtonItem barButtonItemWithTint:[UIColor redColor] andTitle:@"Done" andTarget:self andSelector:@selector(doneClicked)]];
[self pickerView:self.countPeople didSelectRow:0 inComponent:0];
}
我正確使用類方法嗎?
我不敢相信我錯過了...謝謝你的幫助 – OHHO