从视图中删除按钮似乎很简单,但它无效。
MyViewController.h
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@property (strong, nonatomic, readonly, getter = getMyButton) UIButton* myButton;
- (id) init;
- (id) getMyButton;
@end
MyViewController.m
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
@synthesize myButton = _myButton;
- (id) init
{
if([super initWithNibName: nil bundle: nil])
{
_myButton = nil;
}
return self;
}
- (id) getMyButton
{
if(!_myButton) _myButton = [self createMyButton];
return _myButton;
}
- (void) viewDidLoad
{
[super viewDidLoad];
UIButton* myButton = self.myButton;
[self.view addSubview: myButton];
}
- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (UIButton*) createMyButton
{
UIButton *button = [[UIButton alloc] init];
[button setTitle: @"My Button"
forState: UIControlStateNormal];
[button addTarget: self
action: @selector(myAction:)
forControlEvents: UIControlEventTouchUpInside];
button.frame = CGRectMake(10, 10, 100, 40);
return button;
}
- (void) myAction: (id) sender
{
[self.myButton performSelectorOnMainThread: @selector(removeFromSuperview) withObject: nil waitUntilDone: NO];
}
@end
但没有运气。单击该按钮不会执行任何操作。
如果它不是并发问题那么可能是内存管理问题?也许这只是愚蠢的事情,我不知道。
我尝试将以下行放入myAction方法
NSLog(@"Test 1");
if(_startButton.superview) NSLog(@"Test 2");
仅测试1&#39;已记录。也许这是一个线索,但我不知道的是,为什么按钮在添加到视图中时没有超视图,并且在屏幕上可见
其他信息
我不知道,如果这些相关,但也许
我只是updated Xcode
来自开发者计划的最新版本(之前支持iOS 7.0版,现在是7.1版)
我刚刚开始在真正的iPhone上测试应用程序(我在模拟器测试中遇到同样的问题)
在这个问题的同时,我也注意到NSLog功能在AppDelegate applicationHasLaunched方法中不起作用
由于
答案 0 :(得分:2)
您没有为该按钮设置操作。
更改您的createMyButton
方法,如:
- (UIButton*) createMyButton
{
UIButton *button = [[UIButton alloc] init];
[button setTitle: @"My Button"
forState: UIControlStateNormal];
button.frame = CGRectMake(10, 10, 100, 40);
[button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
同样更改myAction
方法,如:
- (void)myAction:(UIButton *)sender
{
[sender performSelectorOnMainThread: @selector(removeFromSuperview) withObject: nil waitUntilDone: NO];
}
答案 1 :(得分:1)
您的代码似乎过于复杂:
我冒昧地重写你的代码:
标头文件
@property (nonatomic,strong) UIButton * button;
代码文件
- (void)viewDidLoad
{
[super viewDidLoad];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.button setTitle:@"MyButton" forState:UIControlStateNormal];
self.button.frame = self.view.bounds;
[self.button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
-(void)myAction:(UIButton*)sender
{
[sender removeFromSuperview];
}
快乐的编码!
修改强>
如果您坚持使用原始代码,请尝试更改
- (id) getMyButton
到
-(UIButton*)myButton
答案 2 :(得分:0)
我确信您的myAction
不会被解雇。因为你忘了addTarget
在createMyButton
方法上添加一行
[button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
答案 3 :(得分:0)
更新:您还将此问题标记为iOS,但您要导入OSX标题
#import "NSViewController.h"
@interface MyViewController: NSViewController
你应该继承自UIViewController
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
您是否通过已发布的初始化代码创建了视图控制器?
- (id) init
{
if([super initWithNibName: nil bundle: nil])
{
_myButton = nil;
}
return self;
}
此处您没有向self
分配任何代码
- (id) init
{
self = [super initWithNibName: nil bundle: nil];
if(self)
{
_myButton = nil;
}
return self;
}
答案 4 :(得分:0)
按钮是一个类家族,应该使用[UIButton buttonWithType:]