13

作成したボタンごとに異なるメソッドを作成したいと思います。viewDidLoadで「FirstImage」メソッドを呼び出そうとしましたが、機能しません。

ViewDidLoadのセレクターに問題があります。パラメータのないvoidメソッドである「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 

この投稿をもう一度編集しましたが、エラーはありません。みなさん、ありがとうございました。理解するのに時間がかかりました:)

4

5 に答える 5

23

次のように使用する必要があり@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];

また、メソッドに大文字の名前を使用することは非常に悪い習慣であることを付け加えたいと思います。

于 2012-12-29T16:48:27.180 に答える
3

使用する必要があります

- (void)viewDidLoad{
    [self createFirstButton:@"First" myAction:@selector(FirstImage)];
}

また

- (void)viewDidLoad{
    [self createFirstButton:@"First" myAction:NSSelectorFromString(@"FirstImage")];
}

@selector(myAction)に置き換える必要がありmyAction、に置き換える(SEL *)必要がありSELます。

あなたのコードで-createFirstButton:myAction:は、呼び出される前に、その 2 番目のパラメーター[self FirstImage]が最初に評価 (呼び出) されます。@selector() は名前を取り、SEL を返します。

于 2012-12-29T16:50:16.747 に答える
0

次のようにメソッドリテラルを変更します

- (void)createFirstButton: (NSString *)myName withAction: (SEL)myAction

宣言と定義の両方。

また、なぜパブリックおよびプライベートと同じメソッドを宣言したのですか?

于 2012-12-29T18:51:01.633 に答える
0

使用する

[self createFirstButton:@"First" withAction:NSSelectorFromString(@"FirstImage")];

メソッド名を次のように変更します。

- (void)createFirstButton:(NSString *)myName: withAction: (SEL) myAction{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn        addTarget:self
                action:myAction
                forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:myName forState:UIControlStateNormal];
    btn.frame = CGRectMake(20, 916, 120, 68);
    [self.view addSubview:btn];
}

また、

可能であれば、同じフレームを持つすべてのボタンがframeこのメソッドを通過するのだろうか。

于 2012-12-29T16:50:41.100 に答える
0

文字列をメソッド名としてセレクターに渡す場合は、NSSelectorFromString を使用します。文字列は「:」で終わる必要があります 例-

    NSString *myName=@"aMethod:"; 
    [button addTarget:self 
               action:NSSelectorFromString(myName)
     forControlEvents:UIControlEventTouchDown];
于 2014-03-13T08:06:15.293 に答える