gpt4 book ai didi

objective-c - UIButton 在 UIScrollView 中不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:45 26 4
gpt4 key购买 nike

我有一个 ScrollView ,我在其中以编程方式创建按钮。触摸时,按钮应该加载另一个 Nib 。问题是,在触摸时,我得到 [ViewController buttonTest]: 无法识别的选择器发送到实例

让我带您看一下代码:

应用委托(delegate).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;
}

View Controller .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];

}

要加载 View ,我有代码:

[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。

关于objective-c - UIButton 在 UIScrollView 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9889335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com