2

プログラムでボタンを作成しているスクロールビューがあります。タッチすると、ボタンは別のペン先をロードすることになっています。問題は、タッチすると、[ViewController buttonTest]: 認識されないセレクターがインスタンスに送信されることです

コードを見てみましょう:

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];

}
4

3 に答える 3

3

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

しかし、あなたのセレクターは@selector(buttonTest). 2tでパラメータなし。

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

addTarget:action:forControlEvents:次のように呼び出しを変更します。

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

または両方の 3 番目の t を削除します。

于 2012-03-27T12:18:32.050 に答える
1

buttonTest: メソッドを実装しましたか (上記のコードには表示されません)。

于 2012-03-27T12:18:00.253 に答える
1

これを試して:

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

buttonTest:ではなく注意してくださいbuttonTest

于 2012-03-27T12:19:02.233 に答える