繁体   English   中英

UIButton在UIScrollView内部不起作用

[英]UIButton not working inside UIScrollView

我有一个scrollview,其中我以编程方式创建按钮。 触摸时,按钮应该加载另一个笔尖。 问题是,在触摸时,我正在获取[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];

}

您的方法称为- (IBAction)butttonTest:(id)sender 三个t和一个参数。

但是您的选择器是@selector(buttonTest) t ,无参数。

这两个方法签名不匹配。

更改addTarget:action:forControlEvents:调用:

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

或删除两者的第三个t。

您是否实现了buttonTest:方法(因为我在上面的代码中没有看到它)?

尝试这个:

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

注意buttonTest:而不是buttonTest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM