我在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顯示正確的文本,但沒有按鈕創建。 我的問題是什麼?
由於
PLZ添加的全部細節,所以我可以幫你 –