2014-10-11 66 views
0

我是新來的ObjC,我試圖在ViewController文件之外創建按鈕(遵循MVC範例)。 我做了什麼:UIButton和UIViewContoller IOS7

.H

@interface MainMenu : UIButton 

-(UIButton *)addButton; 

@end 

.M

@implementation MainMenu 

-(UIButton *)addButton { 
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(50.0, 50.0, 200.0, 75.0)]; 
    [button setBackgroundColor:[UIColor colorWithRed:1.0 
               green:1.0 
               blue:0.0 
               alpha:1.0]]; 
    [button setTitle:@"TITLE" 
      forState:UIControlStateNormal]; 

    return button; 
} 

@end 

在視圖控制器我想在屏幕上創建按鈕,使響應觸摸事件。所以

.m 
@implementation ViewController 

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    MainMenu *menuButton = [[MainMenu alloc] init]; 
    [menuButton addTarget:self 
        action:@selector(ButtonClick) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:menuButton]; 

} 

-(void)ButtonClick { 

    NSLog(@"You clicked on button!!!"); 
} 

@end 

我可以通過在ViewController.m文件中實現所有,但希望我可以然後分離。第二件事:我試圖添加addTarget:action:forControlEvents:方法到MainMenu.m而不是ViewController.m,我無法使它正常工作,因爲我需要傳遞ViewController作爲一個按鈕。目標和方法ButtonClick從ViewController.m作爲選擇器(我猜)

所以我很有趣如何使它正確的根據MVC? 謝謝!

回答

0

他們的方式,你正在做的是有點不對,因爲你繼承的UIButton,然後你必須實例方法Add按鈕,返回按鈕,沒有任何意義。您可以添加Class級別方法或僅添加子類,並添加另一個init方法來滿足您的要求。

更好的是,如果你想創建一個自定義按鈕,要添加目標的行動也做這樣的事情

@interface CustomButton : UIButton 
- (instancetype)initWithTarget:(id)target andAction:(SEL)action; 
@end 
in .m file 

@implementation CustomButton 
- (instancetype)initWithTarget:(id)target andAction:(SEL)action { 
self = [super initWithFrame::CGRectMake(50.0, 50.0, 200.0, 75.0)]; 
if (self) { 
    [self setBackgroundColor:[UIColor colorWithRed:1.0 
              green:1.0 
               blue:0.0 
              alpha:1.0]]; 
    [self setTitle:@"TITLE" 
      forState:UIControlStateNormal]; 
    [self addTarget:target 
      action:action 
      forControlEvents:UIControlEventTouchUpInside]; 
} 
return self; 
} 
@end 

,你可以直接使用它像這樣在任何地方

[self.view addSubView:[CustomButton alloc] initWithTarget:self andAction:@selector(name)]]; 
0

你快到了。這是你所做的正確的:

1)Subclassed UIButton MainMenu類。 (我通常稱之爲MainMenuButton或更具體的東西)

2)在ViewController中設置,創建和配置MainMenu的一個實例。

這裏是你做了什麼錯:

1)沒有實現自定義的init方法

2)試圖從子類內SETFRAME,這應該從視圖控制器

3完成)你需要修改self,它是子類的當前實例,而不是一個名爲button的新變量。

addButton方法永遠不會被調用,因爲沒有什麼可以調用它。在定製一個UIButton子類時,你會想在一個自定義的init方法中完成所有這些。嘗試使用此代碼替換您的Add按鈕方法:

@implementation MainMenu 
-(id)init { 
    self = [super init]; 
    if(self) { 
     [self setBackgroundColor:[UIColor colorWithRed:1.0 
               green:1.0 
                blue:0.0 
               alpha:1.0]]; 
     [self setTitle:@"TITLE" 
       forState:UIControlStateNormal]; 

    } 
    return self; 
} 
@end 

然後在視圖控制器創建MainMenu的子類,設置幀的實例,並配置像前面那樣。當調用[[MainMenu alloc] init]時,將調用此init方法;並在將其返回給ViewController之前配置該子類。

@implementation ViewController 

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    MainMenu *menuButton = [[MainMenu alloc] init]; 
    [menuButton setFrame:CGRectMake(50.0, 50.0, 200.0, 75.0)]; 
    [menuButton addTarget:self 
        action:@selector(ButtonClick) 
     forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:menuButton]; 
} 

-(void)ButtonClick { 
    NSLog(@"You clicked on button!!!"); 
} 

@end 
+0

謝謝!它看起來不錯,但我想將setFrame行移到MainMenu.h會更好。 – johann 2014-10-11 17:56:07

+0

我不同意。你很少想在其子類中設置UIView的框架。最好從添加它的視圖中明確地設置它。假設您希望將來在您的應用程序的不同UIViewContoller中重用MainMenu。你可能想要在UIViewContoller中有一個不同的框架。切勿在子類內設置框架。 – dfmuir 2014-10-11 17:58:42

+0

我理解你的立場,但我可以將Frame作爲參數傳遞,我將在稍後執行。謝謝! – johann 2014-10-11 18:09:02