uibutton addtarget:action:forcontrolevents:結果の結果[nsobjectはセレクターを認識しません:
-
27-09-2019 - |
質問
私はたくさんのことを試しましたが、まだ結果はありませんでした。
したがって、次のボタンをuiviewcontrollerのサブクラスでプログラムで作成しました。
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0.0, 0.0, 110.0, 40.0);
rightButton.titleLabel.font = [UIFont fontWithName:GAME_FONT_NAME_STRING size:20.0];
[rightButton setTitle:@"MyTitle" forState:UIControlStateNormal];
rightButton.backgroundColor = [UIColor clearColor];
[rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[rightButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rightButton];
セレクターは次の場所です。
- (void)myButton;
私はすべてを試しました:
- (void)myButton;
- (void)myButton:(id)sender;
- (void)myButton:(id)sender forEvent:(UIEvent *)event;
- (IBAction)myButton;
- (IBAction)myButton:(id)sender;
- (IBAction)myButton:(id)sender forEvent:(UIEvent *)event;
そしてもちろん、対応するセレクター:
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[rightButton addTarget:self action:@selector(myButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
結果は、常に猛攻撃の例外です - [nsobjectはセレクターを認識しません:]。ただし、プログラムの通常のバックトレースは次のとおりです。
#0 0x92a6bedb in objc_msgSend ()
#1 0x03b0a430 in ?? ()
#2 0x00306b4e in -[UIControl sendAction:to:forEvent:] ()
#3 0x00308d6f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#4 0x00307abb in -[UIControl touchesEnded:withEvent:] ()
#5 0x002bcddf in -[UIWindow _sendTouchesForEvent:] ()
#6 0x002a67c8 in -[UIApplication sendEvent:] ()
#7 0x002ad061 in _UIApplicationHandleEvent ()
#8 0x02498d59 in PurpleEventCallback ()
#9 0x01cabb80 in CFRunLoopRunSpecific ()
#10 0x01caac48 in CFRunLoopRunInMode ()
#11 0x02497615 in GSEventRunModal ()
#12 0x024976da in GSEventRun ()
#13 0x002adfaf in UIApplicationMain ()
それで、そのボタンの問題は何ですか?
PS:iPhone SDK 3.1.3を使用しています
アップデート!
AppDelegateの次のコード(インターフェイスに宣言はありません):
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[test setTitle:@"Title" forState:UIControlStateNormal];
UIImage *bg = [UIImage imageNamed:...];
[test setBackgroundImage:bg forState:UIControlStateNormal];
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:test];
[test release];
[window makeKeyAndVisible];
}
- (void)testAction {
NSLog(@"Write something...");
}
完璧に機能します!
ただし、同じコードで空のuiviewcontrollerを作成する場合:
- (void)viewDidLoad {
UIButton *test = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];
[test setTitle:@"Title" forState:UIControlStateNormal];
UIImage *bg = [UIImage imageNamed:...];
[test setBackgroundImage:bg forState:UIControlStateNormal];
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:test];
[test release];
[window makeKeyAndVisible];
}
- (void)testAction {
NSLog(@"Write something...");
}
この不思議なエラーが発生します。 :-( ヘルプ!
解決 2
ソリューション!
私の問題は、AppDelegateで次のコードを使用したことでした。
UIViewController *controller = [[MyCustomController alloc] init];
[window addSubview:controller.view];
[controller release];
したがって、ビューコントローラーのビュープロパティのみが保持され、他のすべてがパージされました。ビューコントローラーには機能がありませんでした(タッチイベント、インスタンスメソッドなし)。そのため、AddTarget:Action:Forevents:機能しませんでした。
したがって、スマートにしてください - AppDelegateのDeallocでビューコントローラーをリリースします。 ;)
他のヒント
古い投稿であり、おそらくすべてが今までに整理されていますが、クラッシュを引き起こす別の問題があります。
[test addTarget:self action:@selector(testAction) forControlEvents:UIControlEventTouchUpInside]
間違っている。 action:@selector(testAction)
このようにそこに結腸が必要です:
action:@selector(testAction:)
または、認識されていないセレクターのクラッシュが得られます - これがあなたが得ているものです。
MyButtonはどこに宣言されていますか?合成されている場合は、その代わりに
rightbutton = [uibutton buttonwithtype:uibuttontypecustom];
使用する
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
それはうまくいくはずです。そうでない場合は、財産を宣言している方法を投稿してください。