calling a method inside someClass from AppDelegate Objective-C IOS

前端 未结 3 529
后悔当初
后悔当初 2021-01-15 14:40

I\'m trying to call a method that\'s inside someClass from the AppDelegate class. I usually create an instance and then using that instance, call the method. Like so:

<
3条回答
  •  花落未央
    2021-01-15 15:01

    For Example if you want to Create AlertView only Once and Use it in any UiViewController

    So, you can make Method For UIAlertView and Call the Method When you Want

    1)In your appDelegate.h file

    @property (nonatomic, strong) UIAlertView *activeAlertView;
    

    2) In your AppDelegate.m File

    -(void)openAlert
    {
        NSString *strMsgPurchase = @"Write your message";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Buy" message:strMsgPurchase delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy",@"Restore", nil];
        [alert setTag:100];
        [alert show];
        self.activeAlertView = alert;
    }
    

    3) To Call the Method in which uiview you want

    [((AppDelegate *)[[UIApplication sharedApplication] delegate])openAlert];
    

    Note: Define -(void)openAlert Method in Appdelegate.h file

提交回复
热议问题