【问题标题】:UIButton and UIViewContoller IOS7UIButton 和 UIViewContoller IOS7
【发布时间】:2014-10-11 16:50:54
【问题描述】:

我是 ObjC 的新手,我正在尝试在 ViewController 文件之外创建 Button(遵循 MVC 范例)。 我做了什么:

.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

在 ViewController 中,我想在屏幕上创建按钮并使其响应触摸事件。所以

.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 文件中实现 all 来使其工作,但希望我可以分开。上面的代码没有显示按钮

第二件事:我尝试将 addTarget:action:forControlEvents: 方法添加到 MainMenu.m 而不是 ViewController.m 但我无法使其正常工作,因为我需要将 ViewController 作为目标和方法 ButtonClick 从 ViewController.m 传递为一个选择器(我猜)

所以我很感兴趣如何根据 MVC 使它正确? 谢谢!

【问题讨论】:

    标签: objective-c ios7 model-view-controller


    【解决方案1】:

    你的做法有点不对,因为你继承 UIButton 然后你有实例方法 addButton 它返回一个按钮,没有意义。您可以添加类级别的方法,也可以只添加子类并添加另一个 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)]];
    

    【讨论】:

      【解决方案2】:

      你快到了。这是你做对了:

      1) 将 UIButton 子类化为 MainMenu 类。 (我通常会称之为 MainMenuButton 或更具体的名称)

      2) 在 ViewController 中设置、创建和配置 MainMenu 的实例。

      这是你做错了什么:

      1) 没有实现自定义的init方法

      2) 尝试从子类中设置框架,这应该从 ViewController 完成

      3) 需要修改self,它是子类的当前实例,而不是一个叫button的新变量

      永远不会调用 addButton 方法,因为没有任何东西可以调用它。在自定义 UIButton 子类时,您将希望在自定义 init 方法中完成所有这些操作。尝试用以下代码替换您的 addButton 方法:

      @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
      

      然后在 ViewController 中创建 MainMenu 子类的实例,设置框架,并像以前一样进行配置。这个init方法会在你调用[[MainMenu alloc] 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
      

      【讨论】:

      • 谢谢!看起来不错,但我想将 setFrame 行移到 MainMenu.h 会更好。
      • 我不同意。您很少希望从其子类中设置 UIView 的框架。最好从添加它的视图中显式设置它。假设您希望将来在应用程序的不同 UIViewContoller 中重用 MainMenu。您可能希望在该 UIViewContoller 中有不同的框架。永远不要在子类中设置框架。
      • 我理解你的立场,但我可以将 Frame 作为参数传递,稍后我会实现它。谢谢!
      • 是的,你也可以实现一个 initWithFrame 方法。其余的答案对您有帮助吗?
      • 我使用了 C_X 代码,它可以很好地解决一些语法问题。但是再一次看,我注意到您使用 (id)init 而不检查类型而不是 (instancetype)。我希望我能给你 +1,但我的声誉是 0。无论如何,谢谢!
      猜你喜欢
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多