[Objective-C] Modal view in Navigation and TabBar controller projects

A simple way to show in your project a view that is ever modal (with opacity view on background), like the UIAlertView, in fact it is UIAlertView that we use, subclassing it!

How to?

> Create a new Category of UIAlertView

.H

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]#import <uikit /UIKit.h>
@interface UIAlertView (Modal)
– (void) showCustom;
@end[/code]

.M

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]#import "UIAlertView+Modal.h"

@implementation UIAlertView (Modal)

– (void) showCustom
{
UIView *ModalView = [[UIView alloc] initWithFrame:CGRectMake(-10, -50, 300, 200)];
[ModalView setBackgroundColor:[UIColor redColor]];

UILabel *TextLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 250, 20)];
[TextLabel setText:@"I’m a label on a modal view!"];
[TextLabel setTextColor:[UIColor blackColor]];
[TextLabel setBackgroundColor:[UIColor clearColor]];
[ModalView addSubview:TextLabel];
[TextLabel release];

UIButton *CloseBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[CloseBtn setFrame:CGRectMake(100, 120, 100, 40)];
[CloseBtn setTitle:@"Close" forState:UIControlStateNormal];
[ModalView addSubview:CloseBtn];

[self addSubview:ModalView];
[ModalView release];

[self show];
}
@end[/code]

Now, include the category header in your project and call alert view when you want, in this way:

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"message" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
// use showCustom if you want the modal view!
[alert showCustom];
[alert release];[/code]

enjoy.

Rif: albertopasca.it

Alberto Pasca
Latest posts by Alberto Pasca (see all)