Tutorial: UIAlertView Number of Rows

Posted by | Posted in Uncategorized | Posted on 22-02-2010

The UIAlertView class has a few methods that we don’t use a lot because Apple doesn’t want us to. These use the Private API’s that the iPhone has and developers can’t use. For developers, methods that are not in the SDK, but are still there, are known as “undocumented”. What we are going to be doing is using the setNumberOfRows: method which is indeed undocumented. I tried submitting Ball Dodge Lite with this so it can separate two buttons on the death alert, but rejected because of… you guessed.

I recommend not doing this if you submit to the App Store, but Cydia is OK, there is nothing that says “no undocumented methods allowed. Cydia is also just another way to send your app to the public if Apple rejects it. Understand? Let’s get started!

To start off, we need to implement the method into our class file to use it. Start off by adding the following into your class file.


@interface UIAlertView (extended)
- (void)setNumberOfRows:(int)num;
@end

What we do here is load up the interface method and add the void action into the interface. This will now let us use it because it’s inserted into the UIAlertView class. Make sure you do not actually put it inside of the @implementation YourControllerName because now we are working inside of this class, and might give us a couple of errors or warnings. Make sure it’s out like this.


#import "YourControllerName.h"

@interface UIAlertView (extended)
- (void)setNumberOfRows:(int)num;
@end

@implementation YourControllerName

@end

Now you need to have an action set up. Just head into Interface Builder and make an IBAction, connect, save, then add the action in the .m and .h.


@implementation YourControllerName

- (IBAction)actionName {

}

@end

Now we define the alerts title, message, delegate, and other button titles.


- (IBAction)actionName {
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Tutorial"
                                        message:@"Look! Separate rows!"
                                        delegate:nil
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"Continue", nil];
}

Next, have it show, then take it off the memory by releasing it.


- (IBAction)actionName {
    [myAlert show];
    [myAlert release];
}

We haven’t told it to separate the rows of buttons, so we do so now.


- (IBAction)actionName {
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Tutorial"
                                        message:@"Look! Separate rows!"
                                        delegate:nil
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"Continue", nil];
    [myAlert setNumberOfRows:2];
    [myAlert show];
    [myAlert release];
}

There we have it! We now have two rows. This is good if you have a long button title and it won’t fit when both buttons are on one row, then you get the whole message in when there’s two rows.

I hope this tutorial helped you out. Remember that you can’t send it to Apple or it will be rejected. Cya in the next post!

Comments are closed.