[英]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.