Как добавить пользовательский UIbutton в UIAlertView? - PullRequest
0 голосов
/ 14 марта 2012

У меня есть вопрос о настройке UIAlertView

Я настраиваю UIAlertView даже из фона и кнопки.Я добавляю пользовательскую кнопку в нее, но не могу вызвать действие кнопки.Пользовательские кнопки добавляются в UIAlertView, но как я могу добавить к ним действие?Пожалуйста, помогите мне.Это потому что UIAlertView не может позволить нам иметь пользовательские кнопки?

UIButton *firstButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 120, 90, 30)];
UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(180, 120, 90, 30)];

UIImage *btnImage = [UIImage imageNamed:kButtonBlackImage];

[firstButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[firstButton setTitle:@"Don't Favorite" forState:UIControlStateNormal];
firstButton.titleLabel.font = BOLDFONTSMALL;
[firstButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[firstButton addTarget:self action:@selector(cancelFavorite:) forControlEvents:UIControlEventTouchUpInside];

[secondButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[secondButton setTitle:@"Save Favorite" forState:UIControlStateNormal];
[secondButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
secondButton.titleLabel.font = BOLDFONTSMALL;
[secondButton addTarget:nil action:@selector(addFavorite:) forControlEvents:UIControlEventTouchUpInside];  

[alert addSubview:firstButton];
[alert addSubview:secondButton];

Ответы [ 2 ]

0 голосов
/ 14 марта 2012

Вы можете использовать пользовательский вид для UIAlertView.Если есть проблема с вашим secondButton, проблема может быть в том, что вы установили цель как nil, это должно быть self

 [secondButton addTarget:self action:@selector(addFavorite:) forControlEvents:UIControlEventTouchUpInside];

Только что попробовал и заставил работать с:

UIAlertView*testAlert=[[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
[testAlert show];
UIButton*testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setFrame:CGRectMake(0, 0, 40, 40)];
[testButton addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[testAlert addSubview:testButton];

Я могу использовать свой метод someAction.

0 голосов
/ 14 марта 2012
[firstButton addTarget:self action:@selector(cancelFavorite:) forControlEvents:UIControlEventTouchUpInside];

 Add that buttons properly 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...