iPhone SDK: How can you hide an alert box? - iphone

I am using the following code to show an alert box:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Info" message:#"My Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Can you tell me how to hide the alert box again when the phone changes orientation?

First you have to save a reference to the alert in your interface.
#interface MyViewController : UIViewController {
UIAlertView *alert;
}
#property (nonatomic, retain) IBOutlet UIAlertView *alert;
when you create the alert you use
self.alert = [[[UIAlertView alloc] initWithTitle:#"Info" message:#"My Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
and then you have to add another method didRotateFromInterfaceOrientation:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
self.alert = nil;
}

Related

Xcode Point System Iphone Trivia Game

I am a beginner at creating iPhone apps and am trying to incorporate a simple point system for an iphone trivia game. Here is an basic overview of the app:
http://i.stack.imgur.com/nC0CI.png
So basically what I'm trying to do is make the user type the answer in the textfield. After typing an answer, they would click the button "Answer". If the answer is correct, an alert would show up saying "Correct", adding 100. If incorrect, it would say "Incorrect", subtracting 100.
However here is the problem: when you first answer correctly or incorrectly, the number remains at 0. When you click the button a second time, then it would properly increase/decrease. How do you fix this?
Here is my .h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
NSString *stringpoint;
int point;
}
#property (nonatomic, retain) IBOutlet UITextField *textAnswer;
#property (nonatomic, retain) IBOutlet UILabel *pointlabel;
-(IBAction) checkAnswer;
#end
.m file
#synthesize textAnswer, pointlabel;
int point=0;
-(IBAction)checkAnswer {
pointlabel.text = [NSString stringWithFormat:#"%i",point];
if ([textAnswer.text isEqualToString:#"fruit"]) {
point=point+100;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Correct!"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Incorrect"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
point=point-100;
[alert show];
[alert release];
}
}
I am using Xcode 4.3.
You are updating the label that shows points before you calculate the new total, just need to move it to after:
-(IBAction)checkAnswer {
// don't do this here, too early
// pointlabel.text = [NSString stringWithFormat:#"%i",point];
if ([textAnswer.text isEqualToString:#"fruit"]) {
point=point+100;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Correct!"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Incorrect"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
point=point-100;
[alert show];
[alert release];
}
// now you've changed value of points above, so update the label
pointlabel.text = [NSString stringWithFormat:#"%i",point];
}

How to create ui alert view in .pch in objective c

#define kCustomAlert #"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert Back" message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];[alert show];"
How to call this alert in view controller class?
Declare this macro in your pch file:
#define kCustomAlert() [[[UIAlertView alloc] initWithTitle:#"Alert Title" message:#"Alert Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
Macro Call: kCustomAlert();
Alert Macro With Parameter:
#define kCustomAlertWithParam(title,msg) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
kCustomAlertWithParam(#"This is Title",#"This is message");
Alert Macro with Parameters and Target(For the use of: UIAlertView Delegate Methods)
Please set UIAlertViewDelegate for your Controller.
#define kCustomAlertWithParamAndTarget(title,msg,target) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
kCustomAlertWithParamAndTarget(#"This is Title",#"This is message",self);
You need to make a macro function You can't define it like so. Your Syntax is wrong.
Do it in this way:
#define ShowAlert() [[[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
And call it like:
ShowAlert();
You can also pass the parameters:-
#define ShowAlert(myTitle, myMessage) [[[UIAlertView alloc] initWithTitle:myTitle message:myMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show]
And call it like:
ShowAlert(#"YourTitle", #"YourMessage");
Note: I'm not saying that this is good to use just telling the way to do so.
I don't know how to achieve this or is it possible even or not but the alternative for this is to use below method in your AppDelegate.m and declare the method in AppDelegate.h file and then you can call it by creating AppDelegate instance in any class
-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
I know it's a old question, but as UIAlertView is now deprecated, the answers provided will generate a warning.
Here's how you can achieve this with UIAlertViewController :
FROM A VIEW CONTROLLER
#define ShowAlert(title, myMessage) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:myMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; }
Note that this one can only be used in a view controller.
FROM ANYWHERE
If you want to be able to show an alert anywhere, you'll need this one :
#define ShowAlertFromTopMostController(title, myMessage) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:myMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]]; [[Utils topMostController] presentViewController:alertController animated:YES completion:nil]; }
and you'll need to add following method to a Utils class (subclassing NSObject) :
+(UIViewController*) topMostController {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}

Determine PickerView Value After IBAction is pressed

I have an iOS UiPicker working with my desired values and can detect when a new option is selected with the following code:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *aCompany = [arrayColour objectAtIndex:[pickerView selectedRowInComponent:0]];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Picked:"
message:aCompany
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
My question is how do I get the latest from the picker after pressing a button like this:
- (IBAction)DoneButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Picked:"
message:aCompany
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
Set up your pickerView as an IBOutlet (and link using IB) then you can access it outside of the didSelectRow.
#property (nonatomic) IBOutlet UIPickerView *myPickerView;
Then you can use the following line in DoneButton:
NSString *aCompany = [arrayColour objectAtIndex:[self.myPickerView selectedRowInComponent:0]];

ios 5 how to hook lockscreen notification alert with slider?

All !
What class i have to hook lockscreen notification alert with slider ?
no luck with:
%hook SBAwayController
- (BOOL)activateAlertItem:(id)item
{
%orig;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SBAwayController" message:#"activateAlertItem" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return YES;
}
-(void)_pendAlertItem:(id)item
{
%orig;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"SBAwayController" message:#"_pendAlertItem" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
%end
Thanks in advance
I've managed to do this by hooking SBBulletinModalController/SBBulletinBannerController class, addBulletin method
Lockscreen Notification are either shown through push notification or local notification.you can find a lot of tutorial on the same. Here you will find local and push notification programming guide by apple.

How to change UIAlertView cancel button text

I would like to know that how to change UIAlert's cancel button text during runtime.
- (void)showAlertWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelTitle {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:nil];
[alertView show]
[alertView release];
}