Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nic Huang
Courses Plus Student 10,573 Pointslocalize UIAlertView
In my app, I'm using a lot of UIAlertView, so I need to localize every string I use. The below is my way to localize UIAlertView. But I got an exception which says " [__NSArrayM insertObject:atIndex:]: object cannot be nil' " After looking into it, I believe it's otherButtonTitles causing this exception whenever I try to localize otherMessage. If I just assign nil to "otherButtonTitles", everything works OK! But I'm still not sure how can I fix it if I want to localize it, and why this causes exception.
Thank you
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)aMessage delegate:(id)delegate cancelMessage:(NSString *)aCancelMessage otherMessage:(NSString*)otherMessage{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: NSLocalizedString(title, nil)
message: NSLocalizedString(aMessage, nil)
delegate: delegate
cancelButtonTitle: NSLocalizedString(aCancelMessage, nil)
otherButtonTitles: NSLocalizedString(otherMessage, nil), nil];
[alertView show];
}
1 Answer
Unsubscribed User
6,125 PointsYou can use the addButtonWithTitle: method of the UIAlertView and before use you can check the value of the otherMessage parameter.
See below the solution:
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)aMessage delegate:(id)delegate cancelMessage:(NSString *)aCancelMessage otherMessage:(NSString*)otherMessage {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: NSLocalizedString(title, nil)
message: NSLocalizedString(aMessage, nil)
delegate: delegate
cancelButtonTitle: NSLocalizedString(aCancelMessage, nil)
otherButtonTitles: nil];
if (otherMessage != nil) {
[alertView addButtonWithTitle:NSLocalizedString(otherMessage, nil)];
}
[alertView show];
}