Chcę podnieść inną metodę dla każdego utworzonego przycisku. Próbuję wywołać metodę "FirstImage" w viewDidLoad, ale to nie działa.selektor jako parametr w IOS
Mam problem z selektorem w ViewDidLoad. Nie rozpoznałem "FirstImage", który jest metodą void bez parametru.
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]];
}
Co mam zrobić (zmieniłem "CreateFirstButton" na "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
Poprawiłem ten post ponownie i nie ma więcej błędów. Specjalne podziękowania dla wszystkich. Zajęło mi trochę czasu, aby zrozumieć :)
Zastanawiam się - wszystkie przyciski mają tę samą ramkę? –
A także zawiesi się z 'Nierozpoznanym selektorem', ponieważ wiadomość, którą implementujesz, nazywa się' createFirstButton :: ', a nie' createFirstButton: myAction: '. Co więcej, nie potrzebujesz 'SEL *' (podobnie jak "id"): 'SEL' jest wystarczający. Ponadto '@selector (myAction)' nie robi tego, co uważasz za stosowne, użyj po prostu 'myAction' na swoim miejscu. –