ios - alertview closes when another alert appears -


uialertview *alert = [[uialertview alloc] initwithtitle:@"......" message:@"......" delegate:self cancelbuttontitle:@"dismiss" otherbuttontitles:@"ok", nil];  [alert show]; 

i showing alert view add new category using through text field of alert view, when user tapes ok button of alert first check if user has entered or not, if not show alert message let user know text field mandatory, previous alert adding new category disappears.

i want alert stay on screen. should ??

this crash report , code::

uialertview *alert = [[uialertview alloc] initwithtitle:@"add vehicle category" message:@"this gets covered!" delegate:self cancelbuttontitle:@"dismiss" otherbuttontitles:@"ok", nil];             alert.tag = 5;  txtaddvehiclecategory = [[uitextfield alloc]initwithframe:cgrectmake(12, 45, 260, 25)];  [txtaddvehiclecategory setbackgroundcolor:[uicolor whitecolor]];  txtaddvehiclecategory.placeholder = @"enter vehicle category";  txtaddvehiclecategory.contentverticalalignment =       uicontrolcontentverticalalignmentcenter;  [alert addsubview:txtaddvehiclecategory];   cgaffinetransform mytransform = cgaffinetransformmaketranslation(0, -50);  [alert settransform:mytransform];  [alert show]; 

'nsinvalidargumentexception', reason: 'textfieldindex (0) outside of bounds of array of text fields'

when want implement rules entering text in uialertview, should disable ok button of alert view until user follows rules. can achieve uialertviewdelegate methods. alertviewshouldenablefirstotherbutton:.

return no method until rule followed

- (bool)alertviewshouldenablefirstotherbutton:(uialertview *)alertview {     // when return no, ok button disabled.     // when return yes, rule followed , ok button gets enabled.     return ([[[alertview textfieldatindex:0] text] length]>0)?yes:no; } 

edit

the code provided assuming using default alert view style textfield, hence crash.

you should not addsubview uialertview, because view hierarchy of alert view private. recent ios7 not display subviews user adds on uialertview.so recommend not this.

from apple docs,

the uialertview class intended used as-is , not support subclassing. view hierarchy class private , must not modified.

instead, use uialertviewstyle.

following styles supported,

typedef enum {    uialertviewstyledefault = 0,    uialertviewstylesecuretextinput,    uialertviewstyleplaintextinput,    uialertviewstyleloginandpasswordinput } uialertviewstyle; 

use 1 suits requirement. validate input entered user use above method suggested. refer this simple guide/tutorial using alert view these styles.

hope helps!


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -