Akshay
Akshay

Reputation: 425

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];"
  1. How to call this alert in view controller class?

Upvotes: 2

Views: 897

Answers (4)

AnthoPak
AnthoPak

Reputation: 4391

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;
}

Upvotes: 0

PJR
PJR

Reputation: 13180

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);

Upvotes: 7

TheTiger
TheTiger

Reputation: 13354

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.

Upvotes: 2

D-eptdeveloper
D-eptdeveloper

Reputation: 2430

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];
}

Upvotes: 0

Related Questions