selector as parameter in IOS(作为iOS中的参数的选择器)
本文介绍了作为iOS中的参数的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为每个创建的按钮引发不同的方法。 我尝试在viewDidLoad中调用"FirstImage"方法,但不起作用。
我在ViewDidLoad中的选择器有问题。无法识别"FirstImage",它是没有参数的空方法。
ViewController.m
- (void)createFirstButton:(NSString *)myName: (SEL *)myAction{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:myName forState:UIControlStateNormal];
btn.frame = CGRectMake(20, 916, 120, 68);
[self.view addSubview:btn];
}
- (void)viewDidLoad{
[self createFirstButton:@"First" myAction:[self FirstImage]];
}
我做了什么(我将"CreateFirstButton"改为"CreateButton"):
ViewControler.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myHeight;
@synthesize myWidth;
@synthesize myX;
@synthesize myY;
- (void)createButton:(NSString *)myName:(SEL)myAction:(NSUInteger)my_x:(NSUInteger)my_y:(NSUInteger)my_width:(NSUInteger)my_height
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:myAction
forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:myName forState:UIControlStateNormal];
btn.frame = CGRectMake(my_x, my_y, my_width, my_height);
[self.view addSubview:btn];
}
- (void)myXcrementation{
myX = myX + 150;
}
- (void)viewDidLoad{
myX = 20; myY = 916; myWidth = 120; myHeight = 68;
[self createButton:@"First":@selector(FirstImage):myX:myY:myWidth:myHeight];
[self myXcrementation];
[self createButton:@"Previous":@selector(PreviousImage):myX:myY:myWidth:myHeight];
[self myXcrementation];
[self createButton:@"Pause":@selector(PauseImage):myX:myY:myWidth:myHeight];
[self myXcrementation];
[self createButton:@"Next":@selector(NextImage):myX:myY:myWidth:myHeight];
[self myXcrementation];
[self createButton:@"Last":@selector(LastImage):myX:myY:myWidth:myHeight];
}
- (void)FirstImage{
current = 0;
[self SetImage];
}
-(void)SetImage{
[myImageView setImage: [myArray objectAtIndex:(current)]];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
}
@property(assign, nonatomic) NSUInteger myHeight;
@property(assign, nonatomic) NSUInteger myWidth;
@property(assign, nonatomic) NSUInteger myX;
@property(assign, nonatomic) NSUInteger myY;
@end
我重新编辑了这篇文章,没有更多的错误。 特别感谢大家。我花了很长时间才明白:)
推荐答案
您必须按如下方式使用@selector
[self createFirstButton:@"First" myAction:@selector(FirstImage)];
那么您的签名是错误的,因为SEL
不应该是指针。
更改
- (void)createFirstButton:(NSString *)myName: (SEL *)myAction{
至
- (void)createFirstButton:(NSString *)myName: (SEL)myAction{
最后myAction
具有SEL
类型,因此您可以将其直接传递给UIButton
方法,如下所示
[btn addTarget:self
action:myAction
forControlEvents:UIControlEventTouchUpInside];
另外,我想补充一点,对方法使用大写名称是一种非常糟糕的做法。
这篇关于作为iOS中的参数的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:作为iOS中的参数的选择器
基础教程推荐
猜你喜欢
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01