【问题标题】:touchup inside is not working on UIButton里面的修饰在 UIButton 上不起作用
【发布时间】:2016-05-31 22:27:51
【问题描述】:

我正在 Xcode 7.2 中开发一个 iPhone 应用程序没有情节提要。

这是我的第一个屏幕。

我拿了一个 UIButton,把它连接到方法上。

但是我的方法没有被调用。

下面是我的 AppDelegate 代码。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc]
                           initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    //[self.window addSubview:self.viewController.view];

    [self.window makeKeyAndVisible];

我的按钮点击事件:

- (IBAction)loginButton:(id)sender
{
  NSLog(@"Hello world");
}

【问题讨论】:

  • 你能分享创建按钮的代码
  • 我正在将连接从我的按钮拖到代码中,内部进行了修饰。
  • 请发布无效的代码
  • - (IBAction)loginButton:(id)sender{ NSLog(@"Hello world"); }
  • 在登录按钮上点击鼠标右键,进行屏幕显示,需要查看它是否正确连接到文件所有者。

标签: ios objective-c xcode xib


【解决方案1】:

为 UIButton 创建选择器

[button addTarget:self 
       action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

并实现内部方法

-(void)buttonAction:(id)sender  
{//implement code here  }

这是来自 Programmagically 的 UIButton 的所有代码示例(不使用情节提要)

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
  action:@selector(buttonAction:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"LogIn" forState:UIControlStateNormal];
button.frame = CGRectMake(10, 100, 50, 200);
[self.view addSubview:button];

【讨论】:

猜你喜欢
  • 2020-08-07
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多