Objective-c action before closing the window - objective-c

How can i assign an action for close button in my window in objective-c?
For example, user clicks on close button and then program asks: "Do you really want to close the window?"

If you want to add message before closing view in Application you need to implement UIAlertView in it, don't forget to add < UIAlertViewDelegate> in .h file
code in action button pressed;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"Do you really want to close the window?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
If you want to do something when the button is clicked, implement this delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// Write Close Code here.....
}
}

Define an uialertview
UIAlertView *message;
message= [[UIAlertView alloc] initWithTitle:#"LOGOUT!" message:#"Are you sure to Logout of This app?" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
case :
{
[message show];
vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"loginVC"];
[[SlideNavigationController sharedInstance] popAllAndSwitchToViewController:vc withCompletion:nil];
break;}
}
default:
break;
}

Related

UIAlertView On Button Click

Yes, I've already Googled this question and I've used the exact code, but that's still not working.
Here is my current Objective-C code:
- (IBAction)btnTemp:(id)sender
{
if (_deepSwitch.on == TRUE)
{
[self TempCleaner];
_progress.progress += 1;
}
UIAlertView *cleaned = [[UIAlertView alloc] initWithTitle:#"Done!" message:#"Your device is now clean. Restarting SpringBoard." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[cleaned show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView cancelButtonIndex]) {
NSLog(#"clicked");
}
}
Why isn't this working? I've tried buttonIndex 0, and cancel button!
Set the alert view's delegate to self
UIAlertView *cleaned = [[UIAlertView alloc] initWithTitle:#"Done!" message:#"Your device is now clean. Restarting SpringBoard." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
Also, make sure that the class from which you are presenting the alert view declares the UIAlertViewDelegate protocol
//YourClass.h
#interface YourClass : SuperClass <UIAlertViewDelegate>

Pop up window that blocks app in objective C

I want to show a box and proceed with my code based on user's input. UIAlertView does not work as the application does not wait for the input. So is there another type of widget that can wait for user input and then pass control to the app ?
I know there are duplicates here but they are not very helpful as I seem to be missing some pieces (completely new to objective c...)
-(void)function1{
...
// Add UIAlertView *alert; to .h file
alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView == alert && buttonIndex == 1){ // If yes
[self function2];
}
}
-(void)function2{
...
//Continue your task
}
you want a MODAL alert -- just use a UIAlertView and wait for its delegate before proceeding.
- a {
alert.delegate = self;
[alert show];
}
-alertView:(id)a didDismissWithButtonIndex:(int)i {
[self proceed];
}
- proceed {
... after alert ...
}

popViewController doesn't work with UIAlertView

I am having problem with AlertView. I am trying to use the UIAlertView and after click ok it will return back to the previous screen but it do not seems to work any advice ?
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
[self.navigationController popViewControllerAnimated:YES];
}
or
if (xGPSCoordinate==0 && yGPSCoordinate == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iPoly"
message:#"Failed to load the to get your current location"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self.navigationController popViewControllerAnimated:YES];
return;
}
both doesn't work
For this purpose you've to use UIAlertView's delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
First use this in your #interface <UIAlertViewDelegate>
Then set the delegate, self.yourAlertView.delegate=self;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//first button which should be the OK button
{
[self.navigationController popViewControllerAnimated:YES];
}
}
use the delegate method of UIAlertView, see the answer given by iNoob. It does not make a sense if you write anything after the "return;" statement as the code below "return;" statement will never get executed.
refer apple developer link for more details on UIAlertView delegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
or a simple tutorial on alert view
http://mobile.tutsplus.com/tutorials/iphone/uialertview/
You just need to implement UIAlerView Delegate Methods.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{}
write your code for Pop to previous Controller here.You can the clicked button index and on the basis of that you can use.Don't for Conform UIAlertViewDelegate to Interface.

Storyboard does not changes the view after uialertview

I have troubles when I perform a UIAlertView, (the alert view works fine) but... i cannot perform the segue to another window... the type of transition is modal... any help?
if([txtPeticion hasText] )
{
alertaVeladora = [[UIAlertView alloc]
initWithTitle:#"Santuario Virtual"
message:#"Gracias por compartir tu veladora!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[alertaVeladora show];
[self postMessage:txtPeticion.text shareTw:shareTwitter shareFb:shareFacebook withEmail:email];
txtPeticion.text = #"";
[self performSelector:#selector(dismissAlert:) withObject:alertaVeladora afterDelay:1.0f];
}
If I understand correctly, you want to perform the segue when the user presses the dismiss button on the AlertView? To perform an action when the button has been pressed, use the following code:
if([txtPeticion hasText] )
{
alertaVeladora = [[UIAlertView alloc]
initWithTitle:#"Santuario Virtual"
message:#"Gracias por compartir tu veladora!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[alertaVeladora show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
// Perform Segue
[self performSegueWithIdentifier: #"MySegue" sender: self];
}
}

clickedButtonAtIndex: not working

Hi guys i have this i IBAction linked to a button:
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert1 show];
}
and a clickedButtonIndex to auto run but somehow it doesn't load SecondViewController:
#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alert1 clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0){
SecondViewController *sampleView = [[SecondController alloc] init];
[sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:sampleView animated:YES];
}
else{
// Cancel prompt
}
}
Am i missing something here?
If you don’t give the alert view some button titles, there won’t be any buttons to tap on and that delegate method won’t get called.
- (IBAction)showCurl:(id)sender {
alert1 = [[UIAlertView alloc] initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
}
Your code doesn't show a button
alert1 = [[UIAlertView alloc]initWithTitle:#"Loading" message:nil delegate:self cancelButtonTitle:**nil** otherButtonTitles:**nil**];
You pass nil as cancelButtonTitle AND nil as otherbuttonTitles, you should at least have one button title set.