I browsed around the net searching for a code that embeds a "view" in a UIAlertView. It turned out that it was indeed a very popular topic but wasn't addressed. Fortunately, I got it to work.
Logic:
1. Set the view
2. Show the uialertview and it should embed the view and all should be fine.
What I did is to inherit the UIAlertView and then have a UIView * object as a member variable and then create frames to position it correctly.
What I realised later on was that you also needed to scale it, which is accomplished through CGAffineTransform()
The code:
Header:
@interface AlertView: UIAlertView { // Inheriting UIAlertView
UIView *subview; // view we want to embed
}
@property (nonatomic,retain) UIView *subview;
-(void)close:(NSNotification *)pNotification; // will explain later
@end
Code File:
@implementation AlertView
@synthesize subview;
-(id)init
{
if(self=[super init])
{
self.delegate=self; // Set the delegate to handle UIAlertView events to
self.title=@"Title";
[ self addButtonWithTitle:@"Close"]; // add a close button
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close:) name:@"CLOSE" object:nil];
// Declaring an event CLOSE which cause the AlertView to close. This object will listen for this message and do appropriate actions when the event is triggered.
}
return self;
}
// the only funciont where you can adjust the dimensions of the UIAlertView.
- (void)willPresentAlertView:(AlertView *)alertView {
alertView.frame=CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y-100, alertView.frame.size.width, 350.0);
//scale the view
CGAffineTransform resize=CGAffineTransformScale(alertView.transform, alertView.frame.size.width/500, 350.0/alertView.subview.frame.size.height);
[ alertView.subview setTransform:resize];
alertView.subview.frame=CGRectMake(alertView.bounds.origin.x+15.0,alertView.frame.origin.y-40.0, alertView.frame.size.width-30.0, 300.0-80.0);
// embed the our view as a subview
[ alertView addSubview:alertView.subview];
int counter=0;
for ( UIView *view in alertView.subviews)
{
if(counter==1) //counter=0 => label for msg , counter = 1 => our view , counter = 2 => close button
{
view.transform=CGAffineTransformTranslate(view.transform, 0.0, 230.0);
break;
}
counter++;
}
}
-(void)dealloc
{
//deallocate everything
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"CLOSE" object:nil];
[subview dealloc];
[super dealloc];
}
-(void)close:(NSNotification *)pNotification
{
// upon triggering the event CLOSE it will close the alertview.
[self dismissWithClickedButtonIndex:0 animated:YES];
}
How do you invoke the event?
In the action function ( say upon clicking a button or clicking on a row in UITableView), write this code:
[ [ NSNotificationCenter defaultCenter] postNotificationName:@"CLOSE" object:nil ];
usage :
UIView *view= some view;
AlertView *alView= [ [ AlertView alloc] init];
alView.subview=view;
[alView show];
That's it. Its that simple.
Until next time,
Cheers
Tuesday, May 26, 2009
Subscribe to:
Comments (Atom)