0

コントローラーからいくつかのビューコードを構築しようとしています (明らかにペン先を使用せずに)。ということで、簡単な例でやってみたのですが、なぜかコントローラーのボタンにターゲットを追加できず、あとは大丈夫です。これが私が試していることです:

Controller.h:

#import <UIKit/UIKit.h>
#import "IndexView.h"

@interface IndexController : UIViewController
{

}

@property (nonatomic) IndexView *contentView;

@end

Controller.m

#import "IndexController.h"
#import "IndexView.h"

@interface IndexController ()

@end

@implementation IndexController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.contentView = [[IndexView alloc]init];
        [self.view addSubview:self.contentView];

        [self connectUIElements];
    }
    return self;
}

- (void) connectUIElements
{
    [self.contentView.testButton addTarget:self action:@selector(testButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --
#pragma mark UIHandler
- (void) testButtonClicked
{
    NSLog(@"testbutton clicked");
}

@end

View.h

#import <UIKit/UIKit.h>

@interface IndexView : UIView

@property (nonatomic, strong) UIButton *testButton;

@end

View.m

#import "IndexView.h"

@implementation IndexView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self setUpView];
    }
    return self;
}

- (id) init
{
    self = [super init];
    if (self) {

        [self setUpView];
    }
    return self;
}

- (void) setUpView
{
    [self setBackgroundColor:[UIColor whiteColor]];

    self.testButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 50, 100, 50)];
    self.testButton.backgroundColor = [UIColor greenColor];
    [self.testButton setTitle:@"hello, world" forState:UIControlStateNormal];
    [self addSubview:self.testButton];
}

@end

私は、古典的な MVC パターンに近づき、Apple のメディエーターの解釈から少し遠ざかる可能性を探っています。何が問題なのですか?

4

1 に答える 1

0

initWithFrame:のデフォルトの初期化子であるを使用してビューを作成する必要がありますUIView。(理由は簡単ではありませんinitが、私はここで推測しています! - ビューのサイズがゼロであるため、タッチイベントが機能しない可能性があります。)

于 2013-05-23T20:59:15.650 に答える