2017-05-18 5 views
1

UIButtonをクリックしても表示されません。いくつかの図 私は間違って何をしているの?UIViewでUIButtonが応答しない

#import "MyView.h" 

    - (init){ 
     self = [super init]; 
     if(self) 
      [self createButton]; 
     return self; 
    } 

    - (void) createButton{ 
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100,100,100,50)]; 
     [button setTitle:@"Go" forState:UIControlStateNormal]; 
    [button addTarget:self 
        `action:@selector(goAction) 
      forControlEvents:UIControlEventTouchUpInside]; 
     [self addSubview:button]; 
    } 

    - (void) goAction{ 
     NSString *test = @"Some text"; 
    } 

MyViewController.m

- (id) init{ 
     self = [super init]; 
     if (self) { 
      MyView *myview = [[MyView alloc] init]; 
      [self.view addSubview:myview]; 
     } 
     return self; 
    }` 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

ViewController.m

#import "ViewController.h" 
#import "MyViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    MyViewController *myTestView = [[MyViewController alloc] init]; 
    [self.view addSubview:myTestView.view]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 
+0

インライン 'オブジェクトC'を(あなたが別のものに1のViewControllerを追加する必要があり、なぜ私はまだ理解していないが)、それは動作します。この道の、 – user2754113

+0

は、なぜあなたはプログラムで別の' UIViewController'(MyViewControllerを作成してください)?なぜMyViewをViewController.mに直接追加しないのですか? – schmidt9

+0

MyViewをViewControllerに直接追加することは問題ありません。しかし、なぜMyViewControllerをViewControllerに追加しても機能しないのはわかりません! – user2754113

答えて

1

クリックが動作しない理由

MyView.h 

    @interface Myview : UIView 

    @end 

MyView.m:ここに私の全体のコードですあなたが設定していないことです

@implementation MyViewController 

- (instancetype)init 
{ 
    self = [super init]; 
    if (self) { 
     MyView *myview = [[MyView alloc] init]; 
     myview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width); 
     [self.view addSubview:myview]; 
    } 
    return self; 
} 
関連する問題