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

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

Related

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

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

MPMoivePLayerController crash when click full screen button

I use SWRealViewController for my sidebar.
My moviePlayer work fine with out fullscreen, but when i click fullscreen moviePlayer crash and i got an error:
2014-01-01 19:23:00.860 Chingfong[56193:70b] Warning: Attempt to
dismiss from view controller while
a presentation or dismiss is in progress!
Here i my code in movieVC.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface movieVC : UIViewController
{
NSString *link;
}
#property(nonatomic,retain) NSString *link;
#property(nonatomic, strong)UIActivityIndicatorView *activityIndicator;
#property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
#end
and here is movieVC.m
#import "movieVC.h"
#import "SWRevealViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#interface movieVC ()
#end
#implementation movieVC
#synthesize link;
#synthesize activityIndicator;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *urlAddress = link;
//Create a URL object.
NSURL *url1 = [NSURL URLWithString:urlAddress];
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
self.moviePlayer.view.frame = CGRectMake(0,0,320,180);
[self.view addSubview:_moviePlayer.view];
self.moviePlayer.shouldAutoplay = NO;
[self.moviePlayer setFullscreen:YES animated:YES];
}
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[self.moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(#"playbackFinished. Reason: Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(#"playbackFinished. Reason: Playback Error");
break;
case MPMovieFinishReasonUserExited:
NSLog(#"playbackFinished. Reason: User Exited");
break;
default:
break;
}
[self.moviePlayer setFullscreen:NO animated:YES];
}
- (void)showMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *urlAddress = link;
//Create a URL object.
NSURL *url1 = [NSURL URLWithString:urlAddress];
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
self.moviePlayer.view.frame = CGRectMake(0,0,320,180);
[self.view addSubview:_moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}
-(void)viewWillDisappear:(BOOL)animated {
[self.moviePlayer stop];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Playing a video to start the application xcode

Playing a video to start the application
I have this code, I can't see the video and I can hear the audio.
Could anyone explain why ?
-(void)awakeFromNib
{
NSURL *url2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tecnolitevideo_1" ofType:#"mov"]];
moviePlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:url2];
[moviePlayer presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateChanged:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
}
-(void)moviePlayerPlaybackStateChanged:(NSNotification *)notification {
}
- (void)viewDidLoad
{
NSURL* url = [[NSBundle mainBundle] URLForResource:#"3" withExtension:#"mov"];
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
m_player = nil;
}
I am try this and works fine!!!

mpmovieplayercontroller disappears as soon as it starts

When I click “play movie” the mpmovieplayer presents itself for half a second and I see it’s loading movie and the controls are there, but then it returns to main screen without playing the video.
How do I fix this?
Edit:
I have changed code and now the player stays up but is stuck on loading, it doesn’t play movie.
IS the problem: not preparing the movie for play? Or not stopping movie in background and then restarting it?
#import "ViewController.h"
#implementation ViewController
#synthesize moviePlayer;
-(IBAction)grabVid:(id)sender;
{
[self presentModalViewController:imagePicker animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[imagePicker setDelegate:self];
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
imagePicker.sourceType];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
{
[imagePicker dismissModalViewControllerAnimated:YES];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
//[imagePicker dismissModalViewControllerAnimated:YES];
}
-(IBAction)playMovie:(id)sender
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self.view addSubview:playercontroller.moviePlayer.view];
[playercontroller.moviePlayer setFullscreen:YES animated:YES];
playercontroller.moviePlayer.shouldAutoplay = NO;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
//[playercontroller.moviePlayer release];
}
- (void)dealloc {
//[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
-(IBAction) playVideo
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.initialPlaybackTime = 0;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
// playercontroller = nil;
}

Making the view scrollable when keyboard appears

Managed to get it on my first view, but doesnt work on second view.
Here's what I did on both view, with only a slight differencs for debugging purposes in console
-(void) viewWillAppear:(BOOL)animated {
//---registers the notifications for keyboard---
// to see if keyboard is shown / not shown
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
if (keyboardIsShown) return;
NSLog(#"Keyboard is visible 1"); // debugger purpose "Keyboard is visible 2" on the second view.
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue *aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view (with keyboard)---
CGRect viewFrame = [scrollview frame];
NSLog(#"%f", viewFrame.size.height);
viewFrame.size.height -= keyboardRect.size.height;
scrollview.frame = viewFrame;
NSLog(#"%f", keyboardRect.size.height);
NSLog(#"%f", viewFrame.size.height);
//---scroll to the current text field---
CGRect textFieldRect = [currentTextField frame];
[scrollview scrollRectToVisible:textFieldRect animated:YES];
keyboardIsShown = YES;
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the original size
// (without keyboard)---
CGRect viewFrame = [scrollview frame];
viewFrame.size.height += keyboardRect.size.height;
scrollview.frame = viewFrame;
keyboardIsShown = NO;
}
//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
removeObserver: self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
You are registering for UIKeyboardDidShowNotification and UIKeyboardDidHideNotification notifications, and then deregistering for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications. There's your error.

Resources