Present view controller is not working with ios 9,
when I press the button it not redirected to present view controller.
Why this happens ?
I had tried the below code
RegistrationViewController * viewController =[[UIStoryboard storyboardWithName:#"Registration" bundle:nil] instantiateViewControllerWithIdentifier:#"RegistrationViewController"];
[self presentViewController:viewController animated:YES completion:nil];
I had also tried the below code
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Registration"
bundle:nil];
RegistrationViewController *add =
[storyboard instantiateViewControllerWithIdentifier:#"RegistrationViewController"];
[self presentViewController:add animated:YES completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:add
animated:YES
completion:nil];
});
}];
Edit
My complete code here. I am using Xcode 7 and running it with ios 9
#import "LoginViewController.h"
#import "RegistrationViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionRegistrationClicked:(id)sender
{
RegistrationViewController * viewController =[[UIStoryboard storyboardWithName:#"Registration" bundle:nil] instantiateViewControllerWithIdentifier:#"RegistrationViewController"];
[self presentViewController:viewController animated:YES completion:nil];
}
#end
Registration view controller
#import "RegistrationViewController.h"
#interface RegistrationViewController ()
#end
#implementation RegistrationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Make sure the presentedViewController is nil. If presentedViewController is not nil change the code to following.
RegistrationViewController * viewController =[[UIStoryboard storyboardWithName:#"Registration" bundle:nil] instantiateViewControllerWithIdentifier:#"RegistrationViewController"];
if ([self presentedViewController]) {
[[self presentedViewController] dismissViewControllerAnimated:NO completion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:viewController animated:YES completion:nil];
});
}];
} else {
[self presentViewController:viewController animated:YES completion:nil];
}
try viewDidAppear in
-(void)viewDidAppear:(BOOL)animated{
[self presentViewController:viewController animated:YES completion:nil];
}
Try the below code:
RegistrationViewController * viewController = [[self.storyboard instantiateViewControllerWithIdentifier:#"RegistrationViewController"];
[self presentViewController:viewController animated:YES completion:nil];
Related
I am trying to build a splash screen so I want the 1st View Controller to move to 2nd ViwController automatically after 3.0 sec I have tried the below method but an infinite loop has started what should i do ,how should I stop on second view controller.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%p", self);
NSLog(#"1st Controller");
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
});
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#interface LoginViewController : ViewController
#end
//LoginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
see after three seconds how the loop is working .
Why don't you use NSTimer class. Simply create a timer for 10 seconds when 10 seconds passed timer will trigger the event and in that event you can move to another controller. Create a timer like this
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
- (void)targetMethod:(NSTimer*)timer {
[self loadingNextView];
}
It is easy to do, you can add a property to judge if is first come in the vc1:
The result:
In fitst VC:
#import "ViewController.h"
#import "LoginViewController.h"
#interface ViewController ()
#property(nonatomic, assign) BOOL isFirstTime;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_isFirstTime = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
/* [self performSelector:#selector(loadingNextView)
withObject:nil afterDelay:1.0f]; */
if (_isFirstTime == NO) {
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
_isFirstTime = NO;
});
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
Try this
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:false block:^(NSTimer * _Nonnull timer) {
[self loadingNextView];
}];
So a little advise, you should think about presenting a loginViewController instead pushing it to the NavigationController stack. If you push it, you have to remove the back buttons if you don't want the user to come back to the other ViewController. If you present it you can be sure that the user can't go back to the firstVc without entering his login data.
In the second Vc you then can dismiss the Vc or you can present a new ViewController.
[self presentViewController:vc animated:YES completion:nil];
You can achieve you goal by using this.
#import "DrawingViewController.h"
#import "SecondViewController.h"
#interface DrawingViewController ()
#end
#implementation DrawingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[button removeFromSuperview];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(target:) userInfo:nil repeats:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)target:(NSTimer*)timer {
SecondViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
Finally got the answer.
#import "ViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%p", self);
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(#"1st controller");
[self loadingNextView];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadingNextView{
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#import "ViewController.h"
#interface LoginViewController : UIViewController
#end
//LoginViewController.m
#import "LoginViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
OK, I am a NOOB with iPhone and have a simple question I assume. I have found a lot of help out there but am not sure what I am doing wrong. I simply want to take a photo with the camera then move to the next view controller after successful capture and place it in an image view. Got most of this code from here so thanks already but can not seem to get it to work, all different kinds of errors I do not understand. I think it must be that I am killing the view controller then trying to re-instantiate it but am a little lost Please Help.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)TakePhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *submit = #"viewControllerSubmit";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:submit bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
As you can see I have tried a few things. I have tried searching for the errors but tried other things to no avail. Any assistance would be great, thanks in advance.
EDITED VERSION and FULL CODE BELOW HERE
====================================================================
OK, I am posting my full code. I can get the form to go from one to the next as I was simply calling the storyboard name wrong. However I can not get the image I take from the camera to show up on the next form. All I get is an empty ImageView without any errors. What am I missing? Please help been working on this for days and have researched a ton.
First View Controller .M File
#import "ViewController.h"
#import "ViewControllerSubmit.h"
#interface ViewController ()
#end
#implementation ViewController{
UIImage *newImage;
}
- (IBAction)TakePhoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil]; //nil from Null
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"ViewControllerSubmit"]){
ViewControllerSubmit *destViewController = segue.destinationViewController;
destViewController.theNewImage = newImage;
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
newImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
NSString *submit = #"ViewControllerSubmit";
NSString *main = #"Main";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:main bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
First View Controller .H File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *picker;
}
#end
Second View Controller .M File
#import "ViewControllerSubmit.h"
#interface ViewControllerSubmit ()
#property (weak, nonatomic) IBOutlet UIImageView *_image;
#end
#implementation ViewControllerSubmit
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self._image.image = self.theNewImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Second View Controller .H File
#import "ViewController.h"
#interface ViewControllerSubmit : ViewController
#property (strong, nonatomic) UIImage *theNewImage;
#end
Again, no error, the camera comes up, it takes the picture and when I click 'Use Photo' the second View Controller pops up but the image view does not show the image captured.
What am I missing? Any help would be great and again please forgive the ignorance, I am new to iPhone.
You need to write navigation code in completion block...
Try below code,
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[picker dismissViewControllerAnimated:YES completion:^{
NSString *submit = #"viewControllerSubmit";
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:submit bundle:nil];
UIViewController *vc = [mainstoryboard instantiateViewControllerWithIdentifier:submit];
[self presentViewController:vc animated:YES completion:nil];
}];
}
I', performing a basic push segue (working with nibs) and from one view controller to another table view controller, and from some reason the 'Back' button is not appearing, that normally it does appear.
This is how i'm performing the push:
#interface HomeViewController ()
#end
#implementation HomeViewController
- (id)init {
self = [super initWithNibName:#"HomeViewController" bundle:nil];
if (self) {
// Do something
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (IBAction)goToStack:(id)sender {
StackTableViewController *stackViewController = [[StackTableViewController alloc] initWithNibName:#"StackTableViewController" bundle:nil];
[self.navigationController pushViewController:stackViewController animated:YES];
}
In your StackTableViewController, you should set setNavigationBarHidden to NO
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
To make sure the navigation bar reappears every time you navigate away from your view controller (HomeViewController), use this:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
I have two view controller named ViewController and SecondViewController. From the first view controller i m trying to call my SecondViewController after 5 second. but it will giving me black screen instead of my expected view.
Here is my view controller code,
`
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(loadingNextView) withObject:nil afterDelay:5.0f];
}
-(void)loadingNextView{
SecondViewController *SVC = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end`
Please suggest. If i used button for navigation then its work but my needs is to call automatically on timer.
If your SecondViewController is in storyboard instead of alloc init you have use instantiateViewControllerWithIdentifier
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
Dont forget to give the identifier for the controller in the storyboard. So the method will be
-(void)loadingNextView{
[self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
SVC.view.backgroundColor = [UIColor redColor];
[self presentViewController:SVC animated:YES completion:NULL];
}
If you are using storyboard then you cannot initialise the controller with the nib Method "initWithNibName" basically searches for an Xib file, which you do not have as you are using a storyboard, better initialise it in the following way ..
-(void)callNextView{
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"yourStoryBoarddName" bundle:nil];
yourViewController *VC = [storyboard instantiateViewControllerWithIdentifier:identifier];
[self presentViewController:VC animated:YES completion:NULL];
}
If you are not using the storyboard, you can simply init the new controller like this
SecondViewController *SVC = [[SecondViewController alloc]init];
[SVC setBackgroundColor: [UIColor redColor]];
[self presentViewController: SVC animated: YES completion: nil];
You need to initialize the controller exact from the storyboard.
- (void)loadingNetView
{
UIStoryboard storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
NSString *identifier = #"SecondController"; // you need to set identifier in storyboard
SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:identifier];
}
Good day to you guys
I have an application that has a UITabBarController for tabbed-navigation... The view-controllers are mapped to their respective TabItems via a URL, just the same as that of Three20's TTNavigationSample App.
My problem is that inside a view controller of mine, i have a button that calls to another view controller which is also attached to a TabItem. When i trigger the button, the application throws an error. How can I resolve this?
In my TabBarController, i have this inside the viewDidLoad method:
-(void)viewDidLoad {
[self setTabURLs: [NSArrayWithObjects:
#"tt://bulletinBoard",
#"tt://contacts",
nil
]];
}
Sample .m file
#import "HabBarController.h"
#implementation TabBarController
- (void)viewDidLoad {
//these are variables like "tt/feed"
[self setTabURLs:[NSArray arrayWithObjects:
kAppFeedURLPath,
kAppHotURLPath,
kAppPostPhotoURLPath,
kAppGeneralActivityURLPath,
nil]];
}
- (UIViewController*)rootControllerForController:
(UIViewController*)controller {
if ([controller canContainControllers]) {
return controller;
} else {
UINavigationController* navController = [[[UINavigationController
alloc] init] autorelease];
[navController pushViewController:controller animated:NO];
return navController;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.tabBarController.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
#end