poptorootview when click on alerrtview ok - iphone

I have a home view ,when click on that it is going to another view again i am going to another view.when click on a button on that view a modalview will appear and then subsequently 3 more modal views when click on each modalview.when click on the final modalview an alert will appear and when click on that alert i want to show the root homeview.Is it possible
?

Display AlertView using given code snippet:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title message: #"Alert Message"
delegate: self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
Delegate Method implementation :
(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

Sample Code given below:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Alert Message?" message:#"Error......" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK"] autorelease];
[alert show];
The implemention alertView's delegate functions is given below
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//cancel clicked ...do your action
}
else if (buttonIndex == 1)
{
//OK clicked
[self.navigationController popToViewController animated:YES];
}
}

just give the delegate in .h file and after in delegate method of alertview write bellow code..
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[self.navigationController popToRootViewControllerAnimated:YES];///this line is important..
}
else{
// do your action...
}
}
i hope this answer is useful to you..
:)

int c=[self.navigationController.viewControllers count]-4;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:c] animated:YES];

Related

Don't Pop Navigation Controller in Xcode if AlertView

I have a Navigation Controller and when I press the back button I don't want to pop the view but I want to show an UIAlertView.I just want to pop the view after having make a choice on the UIAlertView!
How can I do?
I tried to catch the event 'back button pressed' but with no results :(
Create a UIBarButtonItem inside viewDidload and added it to the navigation bar
UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;
create a method which will be called when the backButton is clicked
-(void)backButtonClicked{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Pop View!" message:#"Are you sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
write this UIAlertView delegate method which will be called when the button in the UIAlertView is clicked
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
NOTE: Dont forget to add UIAlertView delegate in the header file <UIAlertViewDelegate>
You can check for back-button press in the viewWillDisappear method like this (got it from here):
-(void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
Hope this helps!
Implement pop in UIAlertview delegate method clickedButtonAtIndex:
The back button press need to implement only showing the UIAlertview
refer this
follow this code
in button action.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Do you want to open?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert show];
//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//NO
{
//your code
}
else if(buttonIndex==1)//YES
{
// your code
}
}

Multiple alertviews in a single view?

I have an Iphone application in which when i am pressing a button it shows an alertview to chose the background.whichever background user is chosing will be played as the background of the audio clips.But now i need to add another alert before i am showing this alert for giving some warning.after that only i need to pop the second one.but i was done that chosing alert in the didappear of that viewcontroller and set it as a Uialertview delegate.and on the button actions i was doing different actions.Can anybody help me on achieving this?
proAlertView *loginav1=[[proAlertView alloc] initWithTitle:#"title" message:#"Choose a Background to play with this program?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Field",#"Beach", #"Stars",nil];
[loginav1 setBackgroundColor:[UIColor colorWithRed:0.129 green:0.129 blue:0.129 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.625 saturation:0.0 brightness:0.8 alpha:0.8]];
[loginav1 show];
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
//[self play];
//moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
if(actionSheet.tag==123)
{
[self backButtonPressed];
}
}
else if (buttonIndex == 1)
{
videoFile = [[NSBundle mainBundle] pathForResource:#"video-track" ofType:#"mp4"];
[self play];
moviePlayer.scalingMode=MPMovieScalingModeAspectFill;
}
how can i include another alert before this is my question?
Initialize first Alertview
UIAlertView *al1 = [[UIAlertView alloc] initWithTitle:#"Warning!" message:#"Warning Msg!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
al1.tag=1;
al1.delegate=self;
[al1 show];
Implement Delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==1){
// implement button events for first Alertview
if(buttonIndex==1){
//First button clicked of first Alertview
UIAlertView *al2 = [[UIAlertView alloc] initWithTitle:#"Choose BG" message:#"Choose BG?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"1",#"2",#"3", nil];
al2.tag=2;
al2.delegate=self;
[al2 show];
}
}
if(alertView.tag==2){
// implement button events for second Alertview
if(buttonIndex==1){
// First button clicked second Alertview.
}
}
}
Controller Class header
#interface ViewController : UIViewController<UIAlertViewDelegate>{
}
Hope this will fulfill your need !
You can do like this, first display warning message in alertview and when user click OK in alertview then in alertview delegate method write code to display second alertview where user can choose background.

How to show an AlertView with another AlertView

I want to show nested alertViews. Problem, which i am facing in nested alertViews is that when i click an "add" button of first alertView it shows the second alertView, in second alertView i have a textField and a "Save" button. I want to save data when i click on save button and then reload UITableViewData, which is already in the first alertView.
I am new in iphone, so please help me.
You should create your alert views with different tag property so that in delegate method you can easily differentiate which alert view is appeared on the screen.
For example :
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Info"
message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[alert setTag: 1001]; // give different tag to different alert views
[alert show];
[alert release];
Now in delegate method :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1001)
{
// do something
}
eles if (alertView.tag == 1002)
{
}
}
Hope it helps you..

opening previous/back screen in iphone clicking on leftbarbuttonitem showing alert with "ok" and "cancel"

i am working on a navigation based application.On the top of the navigation bar there is a by default back button option.i have implemented a uialertview on click event of the back button.
-(IBAction)gotolevelcontroller:(id)sender//method is declared in leftbarbuttonitem's action selector
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Exit" message:#"Do You want to exit?" delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel", nil];
alert.delegate = self;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
//go to previous screen of navigation control.
//what is code to go to previous screen
NSLog(#"ok");
}
else
{
//remain on same screen of navigation control
NSLog(#"cancel");
}
}
any suggestions?
Thanks
Add this code
if (buttonIndex == 0)
{
[self.navigationController popViewControllerAnimated:YES];
}
You are looking for the poptoviewcontroller method of the navigation controller.
[self.navigationcontroller poptoviewcontroller:<prev-viewcontroler> animated:YES/NO];
Back button pops the current viewController.
so add :
[self.navigationController popViewController Animated:YES];
to buttonIndex == 0
and for cancel.. just type return;

Show Alert in clickedButtonAtIndex?

i need to show a confirm alert after the user press buttonIndex 1 but... if i use popViewcontroller in clickedButtonAtIndex it crash without errors.
The problem is that
[self.navigationController popViewControllerAnimated:YES];
is called before second Alert click...
how to fix?
This is my code:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"OK!"
message:#"Completed"
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
}
}
Set the tag properties of the two UIAlertViews to 1 and 2, respectively. Then, in the delegate method, use if statements to check the tag of the UIAlertView argument.
Example:
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
//check the button index
//create and display the other alert view (set the tag property here to 2)
}
else if (alertView.tag == 2)
{
//pop the view controller
}
}