hi i'm new ios programming , trying port os x apps ios , i'm stuck in "uialertview" coding keep on getting warning "code never executed"
code:
- (ibaction)popup:(id)sender { uialertview *alertview; alertview = [[uialertview alloc] initwithtitle:@"the title" message:@"" delegate:nil cancelbuttontitle:@"cancel" otherbuttontitles:@"a", @"b" , @"c", nil]; [alertview show]; if (@"a") { [sender settitle: @"a"forstate:uicontrolstatenormal]; }else if(@"b") { // line error } }
any fix it, cant without "else" in statement whatever last "if statement"
first foremost, shouldn't use uialertview it's been deprecated in ios 8 in favor of uialertcontroller. it's more straight forward.
intending if-statement do?
reads, you're creating uialertview, showing it. then, after shown, run if block. mean handle feedback alert? if so, you'll have use delegate pattern, can read about, , other common patterns, here. setting delegate class (not nil, 1 creates alert), , implementing correct method:
code:
- (void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex
when button pressed, alert view fire method delegate, letting know button pressed. (0 = a, 1 = b, 2 = c, in case).
now, why getting error... because it's important see happening here.
this always return true, therefore code written in else brackets never run. error telling you. why:
@"a" returns string containing character "a", true because exist. if blocks evaluate statements based on true or false. see issue here?
using comparison allows check against values:
code:
if(@"a" == stringvariable) {
now, tie these 2 concepts - compare buttonindex in alertview:clickedbuttonatindex: determine buttons pressed, , accordingly.
Forums iPhone, iPad, and iPod Touch iOS Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
Comments
Post a Comment