how can I present a UIAlertController above UIViewController.view.window - ios8

Now, I created a UIViewController called viewController, and add a subview called maskView on viewController via:
UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
[self.view.window addSubview:maskView];
and then on maskView there is a button called "deleteBtn", when click on the deleteBtn, I want to present a UIAlertController via:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"mybulb.confirmRemove", #"") message:#"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"cancel", #"取消") style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"confirm", #"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[alertController addAction:confirmAction];
[self presentViewController:alertController animated:YES completion:nil];
but the alertController is not present on the topmost screen, it is below the maskView, how can I show the alertController above the maskView added on UIViewController.view.window?
Thanks so much!

finally I fixed this problem by creating a new window object and switch between this window and the default one, the code like this:
if ([UIAlertController class]) {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"readus.org.title", #"alert title") message:#"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"readus.org.cancel", #"取消") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[appDelegate.window makeKeyAndVisible];
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"readus.org.confirm", #"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//do anything you like...
[appDelegate.window makeKeyAndVisible];
}];
[alertController addAction:cancelAction];
[alertController addAction:confirmAction];
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
and self.alertWindow is defined as:
#property (strong, nonatomic) UIWindow *alertWindow;
- (UIWindow *)alertWindow {
if (!_alertWindow) {
_alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *viewController = [[UIViewController alloc] init];
_alertWindow.rootViewController = viewController;
}
return _alertWindow;
}

Related

Xcode8 iOS9 UIAlertController textfield disapear (iOS8&10 is normal)

Xcode 8 iOS9 UIAlertController textfield disappear.
Before migrate from Xcode7 to Xcode8, it don't have any problem while the app running on iOS9.
iOS9 UIAlertController With UITextfield
iOS10 UIAlertController With UITextfield
Does anyone have this problem?
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"mile" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:getLocalizationString(#"cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];
[alertController addAction:cancelAction];
#weakify(alertController);
#weakify(self);
UIAlertAction *okAction = [UIAlertAction actionWithTitle:getLocalizationString(#"ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
#strongify(alertController);
#strongify(self);
UITextField *textField = alertController.textFields.firstObject;
self.totalMileage = textField.text;
self.mileageLabel.text = self.totalMileage;
if (self.start2DriveDateTime&&![self.start2DriveDateTime isEqualToString:#""]) {
[self getRecommendMaintenanceList];
}
}];
okAction.enabled = (self.totalMileage&&![self.totalMileage isEqualToString:#""]);
[alertController addAction:okAction];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
#strongify(self);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
textField.placeholder = #"mile";
textField.keyboardType =UIKeyboardTypeNumberPad;
textField.text = self.totalMileage;
textField.delegate = self;
}];
[self.navigationController presentViewController:alertController animated:YES completion:nil];

Using UIAlertAction with image instead of text

I want to create a star rating selection component based UIAlertController. My current approach is
UIAlertController * view= [UIAlertController
alertControllerWithTitle:#""
message:#"Minimum Rating"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#""
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
[ok setValue:[UIImage imageNamed:#"starRating_1"] forKey:#"image"];
UIAlertAction* ok2 = [UIAlertAction
actionWithTitle:#""
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
[ok setValue:[UIImage imageNamed:#"starRating_2"] forKey:#"image"];
UIAlertAction* ok3 = [UIAlertAction
actionWithTitle:#""
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
[ok setValue:[UIImage imageNamed:#"starRating_3"] forKey:#"image"];
[view addAction:ok];
[view addAction:ok2];
[view addAction:ok3];
[self presentViewController:view animated:YES completion:nil];
Unfortunately the image gets displayed on the left an an accessory view. Is there any way of using the image in place of the AlertAction title?

Use UIAlertView and UIAlertController for same app

I want to display an alert. But as uialertView is deprecated for iOS 8, what can I do to display the alert for both kind of OS?
Try this code snippet to display the alert view in iOS8.This will definitely work.
- (IBAction)btn1Clicked:(UIButton *)sender {
// Alert style
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"ALERT!" message:#"What will you do?" preferredStyle:UIAlertControllerStyleAlert];
__weak ViewController *wself = self;
// Make choices for the user using alert actions.
**UIAlertAction** *doSomethingAction = [UIAlertAction actionWithTitle:#"I'm doing something" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
__strong ViewController *sself = wself;
sself.alertResponseLabel.text = #"You did something!";
}];
UIAlertAction *doNothingAction = [UIAlertAction actionWithTitle:#"I'm totally ignoring this" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
__strong ViewController *sself = wself;
sself.alertResponseLabel.text = #"OK, just ignore me...";
}];
// Add actions to the controller so they will appear
[**alert addAction:doSomethingAction];
[alert addAction:doNothingAction];**
alert.view.tintColor = [UIColor blackColor];
**[self presentViewController:alert animated:YES completion:nil];** }

iOS6 problems to Tweet in UITabBarController using image taken from UIImagePickerController

After picking image from UIImagePickerController I want to Tweet it but there is error!
Warning: Attempt to present <SLTwitterComposeViewController: 0x210d84b0> on <UITabBarController: 0x1fd67650> while a presentation is in progress!
P.S
This is tabbar application (4 tabbars)
All code:
-(void)useCamera:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *imagePicker =[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = #[(NSString *) kUTTypeImage];
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
_newMedia = YES;
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
[self buttonTweet:info[UIImagePickerControllerOriginalImage]];
}
}
- (IBAction)buttonTweet:(id)sender {
SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[composeController setInitialText:[NSString stringWithFormat:#"#this is tweet text"]];
[composeController addImage:sender];
[composeController addURL: [NSURL URLWithString:#"http://www.abc.com"]];
[self presentViewController:composeController animated:YES completion:nil];
}
You are presenting a new view controller while the old one is still disappearing. You can present the new one upon completion of the other animation by doing it like this:
[self dismissViewControllerAnimated:YES completion:^{
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
[self buttonTweet:info[UIImagePickerControllerOriginalImage]];
}
}];

How to make actionsheet go to another view? Xcode, small issue,,,

I am trying to make an actionsheet for another button but I get two errors: "No visible #interface for 'UIstoryboard' declares the selector 'InitiateViewControll:' and, "No visible #interface for SWViewController' declares the selector 'presentViewController:animated:' I was giving this code so is there any names that I need to name in this to make this code costomized for my code? I am trying to learn i am a noob sorry :( here is the code I have so far:
- (IBAction)OpenActionSheetButton:(id)sender {
UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:#"There is no going back, are you sure???" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Continue" otherButtonTitles:nil, nil];
[actionsheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
UIViewController *controller = [self.storyboard instantiateViewControll:#"storyboardViewName"];
//Push
[self.navigationController pushViewController:controller animated:YES];
//Modal
[self presentViewController:controller animated:YES];
}
}
Please help guys?
Try out this code..And SWViewController must have a Navigation Controller
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"storyboardViewIdentifier"];
//storyboardViewIdentifier is the ViewController identifier you specify in the storyboard
//PUSH
[self.navigationController pushViewController:controller animated:YES];
//Modal
[self presentViewController:controller animated:YES completion:Nil];
}
}
Try this. It works for me:
if(buttonIndex == 0)
{
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
Paypal_ViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"Paypal_ViewController"];
[self presentViewController:vc animated:YES completion:nil];
}

Resources