2012-03-27 13 views
2

私はプログラムでボタンを作成しているスクロールビューを持っています。オンタッチでは、ボタンは別のペン先をロードするはずです。問題は、touch上で、私は [ViewControllerをbuttonTest]取得していますされています。認識されていないセレクタはインスタンスにUIButtonがUIScrollView内で動作しない

を送った私は、コードを通してあなたを見てみましょう:

appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) UINavigationController *viewController; 

appdelegate.m

#import "ViewController.h" 
@implementation AppDelegate 
@synthesize window = _window; 
@synthesize viewController = _viewController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    ViewController *view = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.viewController = [[UINavigationController alloc] initWithRootViewController:view]; 
    self.window.rootViewController = self.viewController; 
    [application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

Viewcontroller.h

@class newView; 

@interface ViewController : UIViewController{ 

    UIScrollView * scrollView; 
    IBOutlet UIButton *btn; 

} 

- (IBAction)butttonTest:(id)sender; 
    @property (strong, nonatomic) newView *secondView; 

、最終的には、ViewController.m

- (void)loadView { 

    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame]; 
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect]; 
    scrollView.contentSize=CGSizeMake(320,960); 
    UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpeg"]]; 

    UIImage * buttonImage = [UIImage imageNamed:@"contentlist_active.png"]; 

    self.view=scrollView; 
    [scrollView addSubview:tempImageView2]; 
    scrollView.userInteractionEnabled = YES; 
    btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btn.frame = CGRectMake(22, 100, 277, 32); 
    [btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
    [btn setTitle:@"hello world" forState:UIControlStateNormal]; 
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 

    [btn addTarget:self action:@selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside]; 

    [scrollView addSubview:btn]; 

} 

は、ビューをロードするために、私はコードを持っている:私は間違って

[btn addTarget:self action:@selector(buttonTest) forControlEvents:UIControlEventTouchUpInside]; 

何をしているのですか?

ああ、ここbuttenPressed

- (IBAction)butttonTest:(id)sender { 
self.secondView = [[newView alloc] initWithNibName:@"newView" bundle:nil]; 

[self.navigationController pushViewController:self.secondView animated:YES]; 

} 

答えて

3

あなたのメソッドは- (IBAction)butttonTest:(id)senderと呼ばれます。 3つのtと1つのパラメータ。

セレクタは@selector(buttonTest)です。 2つのtとパラメータなし。

2つのメソッドシグネチャが一致しません。

[btn addTarget:self action:@selector(butttonTest:) forControlEvents:UIControlEventTouchUpInside]; 

または両方の第三トンを削除します。

は、このような addTarget:action:forControlEvents:コールを変更

[btn addTarget:self action:@selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside]; 

buttonTest:ではなくbuttonTest

+0

ええ、感謝します。 –

1

ための方法は、あなたがbuttonTestを実装しているされて:メソッド(私は上記のコードでそれを参照してくださいいけないと)?

+0

私はコードをここで言及するのを忘れていました。私はタンクスをしました: –

関連する問題