2

我有一个滚动视图,我在其中以编程方式创建按钮。触摸时,按钮应该加载另一个笔尖。问题是,在触摸时,我得到 [ViewController buttonTest]: unrecognized selector sent to instance

让我带你看一下代码:

应用代理.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;
}

视图控制器.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。三t和一个参数。

但是您的选择器是@selector(buttonTest). 两个t没有参数。

这两个方法签名不匹配。

像这样改变addTarget:action:forControlEvents:调用:

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

或删除两者上的第三个 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 回答