2010-10-20 111 views

回答

1

我只需要將layoutSubviews中的所有邏輯移至構造函數。

+0

我有同樣的問題老兄,你可以粘貼你的解決方案代碼嗎? – 2014-04-03 11:08:32

0

確保你正在做這樣的事情:

 UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom]; 
     btn.frame = CGRectMake(0, 0, 100, 40); 
     [btn setTitle:@"Click Me" forState: UIControlStateNormal]; 
     [btn setBackgroundColor: [UIColor blackColor]]; 
     [btn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; 
     [btn addTarget:self action:@selector(buttonTap:) forControlEvents: UIControlEventTouchUpInside]; 
     [self.view addSubview:btn]; 

這將創建一個按鈕,單擊時調用buttonTap方法。

+1

目標是一個視圖控制器方法。我在視圖控制器中添加按鈕的目標。我的問題是UIControlEvent沒有被「註冊」。 – 2010-10-22 04:12:46

+0

發佈您的代碼。也許我們(堆棧溢出社區)可以通過查看它來發現錯誤。 – Axeva 2010-10-24 01:19:09

+0

我的代碼如下。 – 2010-10-25 01:02:42

0

這裏是定製視圖和視圖控制器邏輯

//定製視圖

@implementation CustomUIASView 

@synthesize firstButton; 
@synthesize secondButton; 
@synthesize thirdButton; 

- (id)initWithFrame:(CGRect)frame { 

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

    self.alpha   = .85; 
    self.backgroundColor = [UIColor blackColor]; 
} 

    return self; 
} 


- (void)layoutSubviews { 

    UIImage *buttonImageNormal; 
    UIImage *stretchableButtonImageNormal; 
// 
    buttonImageNormal   = [UIImage imageNamed:@"as-bg.png"]; 
    stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

    self.firstButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    self.firstButton.frame   = CGRectMake(10, 10, 300, 50); 
    self.firstButton.backgroundColor = [UIColor clearColor]; 

    [self.firstButton setTitleColor:RGB(16,62,30) forState:UIControlStateNormal]; 
    [self.firstButton setTitle:@"Watch Video" forState:UIControlStateNormal]; 
    [self.firstButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; 

    self.firstButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f]; 

    [self addSubview:self.firstButton]; 

    self.secondButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    self.secondButton.frame   = CGRectMake(10, (10 + 50 + 5), 300, 50); 
    self.secondButton.backgroundColor = [UIColor clearColor]; 

    [self.secondButton setTitleColor:RGB(16,62,30) forState:UIControlStateNormal]; 
    [self.secondButton setTitle:@"Save to My Favorites" forState:UIControlStateNormal]; 
    [self.secondButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; 

    self.secondButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f]; 

    [self addSubview:self.secondButton]; 

    self.thirdButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    buttonImageNormal   = [UIImage imageNamed:@"social-button-bg.png"]; 
    stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

    self.thirdButton.frame   = CGRectMake(10, (secondButton.frame.origin.y + secondButton.frame.size.height + 5), 300, 50); 
    self.thirdButton.backgroundColor = [UIColor clearColor]; 

    [self.thirdButton setTitleColor:RGB(16,62,30) forState:UIControlStateNormal]; 
    [self.thirdButton setTitle:@"Share with Friends on" forState:UIControlStateNormal]; 
    [self.thirdButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; 
    [self.thirdButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 

    CGRect thirdButtonFrame = thirdButton.titleLabel.frame; 

    self.thirdButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f]; 
    self.thirdButton.titleEdgeInsets = UIEdgeInsetsMake(thirdButtonFrame.origin.x, thirdButtonFrame.origin.y + 20, 0, 0); 

    [self addSubview:self.thirdButton]; 

    [super layoutSubviews]; 
} 

- (void)dealloc { 
    [firstButton release]; 
    [secondButton release]; 
    [thirdButton release]; 
    [super dealloc]; 
} 

@end 

//視圖控制器片斷

self.uiasView = [[CustomUIASView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height + 270), kDefaultFrameWidth, 300)]; 

[self.uiasView.firstButton addTarget:self action:@selector(pushToRunwayViewController:) forControlEvents:UIControlEventTouchUpInside]; 

[self.view addSubview:self.uiasView]; 

「pushToRunwayViewController」 不會被調用

+2

我忘了發佈我的解決方案。我只需要將layoutSubviews中的所有邏輯移除到initWithFrame即可。現在我的控制器中的方法被調用。 – 2011-01-13 22:41:18

+0

視圖生命週期和委託方法很古怪。感謝發佈這個解決方案,我遇到了同樣的問題,並且將其揪住了。我猜layoutSubviews是在viewControllers viewDidLoad方法之後調用的。 +1 – atreat 2012-02-21 20:31:24

0

不知道你是否想出了自己,但當你添加T. arget:self到你的firstButton,self是你實際的自定義視圖,而不是視圖控制器。

因此,如果您將您的(pushToRunwayViewController)方法移動到CustomAISView,則所有內容都應按預期工作。

我希望這會有所幫助。 Rog

+0

自我確實指的是視圖控制器。我的練習的目的是創建一個自定義的「操作表」,該操作表將在整個應用程序中使用,而無需將控制器特定的實現與其綁定。必須將pushToRunwayViewController放入類中才能打破抽象。不過,我得到了一切工作(見上面的評論)。我會很快發佈一篇關於它的博客。 – 2011-01-13 22:44:30

-1

首先,UIView與UIControl不同。 UIView並不知道如何處理不同的用戶交互,也沒有實現大部分的事件處理方法。

UIControl爲您做到了。其顯着的子類是UIButton。嘗試從UIControl子類獲取事件處理的所有方法。對於更復雜的方法,在UIView子類的子類中,有另一個UIControl,但我不認爲它是一個好主意。

更多一點點高級事件處理和消息傳遞看看這個:

How to add UIViewController as target of UIButton action created in programmatically created UIView?