【问题标题】:Add Target For UIButton Added to UIView Subclass Not Working on UIViewController为 UIButton 添加目标添加到 UIView 子类在 UIViewController 上不起作用
【发布时间】:2010-10-20 14:14:00
【问题描述】:

我有一个自定义 UIButton 作为子视图添加到 UIView 子类。在我的 viewController 中,我将自定义 uiview 作为子视图添加到控制器的视图中。然后我尝试将目标添加到 uibutton(在 viewWillLoad 内),但从未调用过选择器。

【问题讨论】:

    标签: uiview uiviewcontroller uibutton


    【解决方案1】:

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

    【讨论】:

    • 我也有同样的问题,请问您可以粘贴您的解决方案代码吗?
    【解决方案2】:

    确保你正在做这样的事情:

            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 方法。

    【讨论】:

    • 目标是一个视图控制器方法。我在视图控制器中添加按钮的目标。我的问题是 UIControlEvent 没有被“注册”。
    • 发布您的代码。也许我们(Stack Overflow 社区)可以通过查看来找到错误。
    【解决方案3】:

    这是自定义视图和视图控制器逻辑

    //自定义视图

    @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
    

    //视图控制器sn-p

     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”永远不会被调用

    【讨论】:

    • 我忘记发布我的解决方案了。一切正常我只需要从 layoutSubviews 中删除所有逻辑到 initWithFrame。现在我的控制器中的方法被调用了。
    • 视图生命周期和委托方法很难习惯。感谢您发布此解决方案,我遇到了同样的问题,这解决了它。我猜 layoutSubviews 是在 viewControllers viewDidLoad 方法之后调用的。 +1
    【解决方案4】:

    不确定您是否自己想通了,但是当您将Target:self 添加到您的firstButton 时,self 是您实际的自定义视图,而不是视图控制器。

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

    我希望这会有所帮助。 罗格

    【讨论】:

    • self 确实指的是视图控制器。我练习的目的是创建一个自定义的“操作表”,它可以在整个应用程序中使用,而无需将控制器特定的实现绑定到它。必须将 pushToRunwayViewController 放在类中会破坏该抽象。但是我让一切正常(见上面的评论)。我很快就会发布一篇关于它的博客。
    【解决方案5】:

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

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

    更多高级事件处理和消息传递看这个:

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

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 1970-01-01
      • 2015-09-06
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多