为UIVutton添加目标添加到UIView子类不适用于UIViewController

时间:2010-10-20 14:14:01

标签: uiview uiviewcontroller uibutton

我将自定义UIButton作为子视图添加到UIView子类中。在我的viewController中,我将自定义uiview作为子视图添加到控制器的视图中。然后我尝试将目标添加到uibutton(在viewWillLoad中),但永远不会调用选择器。

5 个答案:

答案 0 :(得分:1)

我只需要将layoutSubviews中的所有逻辑移动到构造函数。

答案 1 :(得分: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方法。

答案 2 :(得分: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”

答案 3 :(得分:0)

不确定你是否为自己弄明白但是当你将firstTarget:self添加到你的firstButton时,self就是你的实际自定义视图,而不是视图控制器。

因此,如果将(pushToRunwayViewController)方法移动到CustomAISView,一切都应该按预期工作。

我希望这会有所帮助。 ROG

答案 4 :(得分:-1)

首先,UIView与UIControl不同。 UIView不具体知道如何处理不同的用户交互,也没有实现大多数事件处理方法。

UIControl为您做到了这一点。其值得注意的子类是UIButton。尝试从UIControl继承子类以获取事件处理的所有方法。对于一个更复杂的方式,在UIView子类中继承了另一个UIControl,但我认为这不是一个好主意。

更多一点高级事件处理和消息传递看看:

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

相关问题