【发布时间】:2015-09-15 18:08:01
【问题描述】:
我在 ViewController 中有一个方法来绘制一个按钮。在 ViewController2 中,我想调用该方法并绘制按钮。
在 ViewController.h 中
@interface ViewController : UIViewController
-(void)method;
@end
ViewController.m
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)method{
UIButton*Touch1= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[Touch1 addTarget:self action:@selector(TouchButton1:) forControlEvents:UIControlEventTouchUpInside];
[Touch1 setFrame:CGRectMake(50,50, 100, 100)];
Touch1.translatesAutoresizingMaskIntoConstraints = YES;
[Touch1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[Touch1 setExclusiveTouch:YES];
[self.view addSubview:Touch1];
NSLog(@"hi ");
}
-(void)TouchButton1:(UIButton*)sender{
NSLog(@"hi again");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然后我尝试从 ViewController2 调用
- (void)viewDidLoad {
[super viewDidLoad];
ViewController * ViewCon = [[ViewController alloc]init];
[ViewCon method];
}
NSLog 显示正确的文本,但没有创建按钮。 我的问题是什么?
谢谢
【问题讨论】:
-
请添加完整的详细信息,以便我可以帮助您
标签: ios objective-c class uibutton