Cannot push a viewController inside a ViewDidload method in another Viewcontroller - ios7

I want to do something like this in my iOS application.
I alredy have a splash screen and a login screen,, After the Splashviewcontroller it displays the LoginViewcontroller.
Im checking some conditions inside the ViewDidloadand in a particular condition I want to push to an another viewcontroller. This is working in ios 5,and 6 but in ios 7 this is not loading when this condition is true.
This is my code
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBar.hidden=YES;
mutArrLoggedUsrarray=[[NSMutableArray alloc]init];
newuser=[NEWLoginUSER sharedManager];
nws=[NEWWebservice sharedManager];
alertObject=[AlertsString sharedManager];
alert=[CreateProgressAlert sharedManager];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *strLogStatus=[defaults objectForKey:#"LOGWAY"];
if ([strLogStatus isEqualToString:#"EMAIL"]) {
phoneLiginView.hidden=YES;
emailLoginView.hidden=NO;
isCurrentViewPhone=NO;
KeychainItemWrapper *keychainItemPHONE = [[KeychainItemWrapper alloc] initWithIdentifier:#"PHONENO" accessGroup:nil];
NSString *userTypedEmail=[keychainItemPHONE objectForKey:(__bridge id)(kSecAttrAccount)];
NSString *userTypedPassword=[keychainItemPHONE objectForKey:(__bridge id)(kSecValueData)];
newuser.strEmail=userTypedEmail;
TxtEmail.text=newuser.strEmail;
// [changeBoxbtn setTitle:#"LOGIN WITH PHONE NUMBER" forState:UIControlStateNormal];
changeBoxbtn.hidden=YES;
}
else if([strLogStatus isEqualToString:#"PHONE"])
{
txtUsrname.text=[newuser.strPhoneNum stringByReplacingOccurrencesOfString:#"+94" withString:#""];
isCurrentViewPhone=YES;
//[changeBoxbtn setTitle:#"LOGIN WITH EMAIL ID" forState:UIControlStateNormal];
changeBoxbtn.hidden=YES;
}
else{
isCurrentViewPhone=YES;
RegisterViewController *regvieww=[[RegisterViewController alloc] initWithNibName:#"RegisterViewController" bundle:nil];
[regvieww.view addSubview:regvieww.viewVerify];
[regvieww.view addSubview:regvieww.viewTop];
[regvieww.view addSubview:regvieww.regViaEmailView];
[regvieww.view addSubview:regvieww.selectorView];
[self.navigationController pushViewController:regvieww animated:YES];
}
originalCenter=self.view.center;
if ([UIScreen mainScreen].bounds.size.height==480) {
originalCenter.y=240;
}
}
Can u tell me what is the problem with this. And how to solve this

Try moving your conditions to your AppDelegate in the method:
didFinishLaunching
And set the first viewController accordingly.
You can also try moving your code from viewDidLoad to viewWillAppear.
Pushing a viewController animated in viewDidLoad won't work.

Related

What's the difference between the back button on UINavigationController and PopTopViewController?

I have a UINavigationController hierarchy, VC1 --> VC2.
VC1 has a table view that I need to reload when VC2 is done with its work, so VC1 has this code:
-(void)viewWillAppear:(BOOL)animated
{
[[self tableView] reloadData];
}
VC2 is essentially working with the server to create a new table row in VC1. When the done button in VC2 is pressed, I call [navController popViewControllerAnimated:YES]. So here's what happens from the user's perspective:
Visit VC2, use it to create a new row for the table in VC1. Press done.
The hierarchy successfully navigates back to VC1, but the tableview does not reload and display the new row.
However, if I then nav forward to VC2, and immediately hit the navController back button, the table does reload and show the new row.
So why does [tableview reload] work on 3 but not 2? Thanks so much.
==
More code in response to answer mentioned below:
In App delegate:
CWLandingVC *lvc = [[CWLandingVC alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:lvc];
[[self window] setRootViewController:navController];
In VC0:
-(void)toSessionMgmtViewController
{
TSessionMgmtViewController *tsmvc = [[TSessionMgmtViewController alloc] init];
[[self navigationController] pushViewController:tsmvc animated:YES];
}
In VC1:
- (IBAction)toCreateSessionView:(id)sender
{
TCreateSession *cs = [[TCreateSession alloc] init];
[[self navigationController] pushViewController:cs animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self tableView] reloadData];
}
In VC2:
Finishes working with server...
UINavigationController *navControler = [self navigationController];
[navControler popViewControllerAnimated:YES];
Also, when VC2 is done working with the server, it updates a data store of TSessions called SessionListStore:
- (TSession *)addSession:(NSString *)code withName:(NSString *) name qs:(int)qs
{
TSession *s = [[TSession alloc] initWithName:name code:code numberQuestions:qs];
[_sessions setObject:s forKey:code];
return s;
}
where sessions is a NSNutatbleDictionary in SessionListStore.
Thanks so much in advance.
EDIT: The solution was to trigger the reloadData call from the completion block of Server call.
Please check this answer,
Popping ViewController doesn't call viewWillAppear when going back
Have you added navigation controller to your view controller or view controllers to your navigation controller?
Also, you can set the desired view controller as the delegate of your navigation controller and implement this method.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

QLPreviewController only shows a single file

I am using a QLPreviewController to display a set of files. However, it only shows the first one and I can't seem to swipe or do anything to show the second. What am I doing wrong? Do I have to set it manually? If so - how would I go about doing that?
This is from my AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// normal viewcontroller init here
[self showPreview] ;
return YES;
}
NSArray* documents ;
QLPreviewController* preview ;
- (void) showPreview
{
documents = [[NSArray alloc] initWithObjects: #"photo" , #"photo2" , nil ] ;
preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
preview.view.frame = [[UIScreen mainScreen] bounds];
//save a reference to the preview controller in an ivar
// self.previewController = preview;
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
preview.view.userInteractionEnabled = YES;
//add it
[self.viewController.view addSubview:preview.view];
}
I also declared the two callback functions in the same AppDelegate.m file:
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
NSString* filename = [documents objectAtIndex:index] ; // #"photo" ;
NSURL* returnURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: filename ofType:#"jpg" ]] ;
return returnURL ;
}
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
You are displaying it wrong.
QLPreviewController is a UIViewController, which means you basically have 2 ways of displaying it:
Push it into your UINavigationController.
Display it modally (this can be done with or without a UINavigationController - depends if you want a navigation bar).
If you choose option 2 you get "free" navigation arrows to switch between items.
For option 1 you need to create the arrows yourself.
This following is taken from the QLPreviewController documentation:
If there is more than one item in the list, a modally-presented (that
is, full-screen) controller displays navigation arrows to let the user
switch among the items. For a Quick Look preview controller pushed
using a navigation controller, you can provide buttons in the
navigation bar for moving through the navigation list.

Reading touch events in a QLPreviewController

I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?
I tried the following, but no events seem to trigger when I test it.
/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>
#interface TouchPreviewController : QLPreviewController
#end
//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"
#implementation TouchPreviewController
- (id)init:(CGRect)aRect {
if (self = [super init]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerDTap];
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
//[singleFingerDTap release];
}
return self;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
NSLog(#"TouchPreviewController tap!" ) ;
}
// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
return YES;
}
#end
Edit: For clarification, this is how I instantiate the controller:
documents = [[NSArray alloc] initWithObjects: filename , nil ] ;
preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width;
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
//add it
[quickLookView addSubview:preview.view];
Also, I've defined the callback methods as this:
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}
Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?
Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since
gesture recognizer is not active.
I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)
I have never used the class so I could be way out in left field.
If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.
I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.
I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.
When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.
Hope this is of some help.

UINavigationController Title Overlap

I am using a UINavigationController and pushing/popping UIViewControllers onto it. In some instances I am attempting to pop to the root view controller and then push a view controller after a short delay (0.1f).
My push code for the Message View Controller is as follows. My app fires two notifications. The first to select a tab and the second to push the correct view controller onto the stack of that tab.
//user taps a button and the app needs to switch tab and push the correct viewController
//onto the tab. I have tried setting pop == NO to avoid a 'double pop' but I still get
//overlapped titles
-(IBAction)messages:(id)sender {
NSDictionary* dictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInt:4], [NSNumber numberWithBool:YES] , nil] forKeys:[NSArray arrayWithObjects:#"tab",#"pop", nil]];
[[NSNotificationCenter defaultCenter] postNotificationName:kAutoSelectTab object:dictionary];
[[NSNotificationCenter defaultCenter] performSelector:#selector(postNotificationName:object:) withObject:kMessages afterDelay:0.1f];
}
//responds to the first notification
-(void)autoSelectTab:(NSNotification*)notification {
NSDictionary* dictionary = (NSDictionary*)[notification object];
int tab = [[dictionary objectForKey:#"tab"] intValue];
BOOL pop = [[dictionary objectForKey:#"pop"] boolValue];
[self.tabBarController setSelectedIndex:tab];
UIViewController* vc = [[self.tabBarController childViewControllers] objectAtIndex:tab];
PSLogDebug(#"Selecting tab:%#",[vc class]);
[self tabBarController:self.tabBarController didSelectViewController:vc];
if (pop == YES) {
if ([vc isKindOfClass:[UINavigationController class]]) {
[(UINavigationController*)vc popToRootViewControllerAnimated:YES];
}
}
}
//responds to the second notification
-(IBAction)messages:(id)sender {
[self.navigationController popToRootViewControllerAnimated:NO];
MessagesViewController* vc = [[MessagesViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
Functionally the views appear to pop and push correctly BUT the titles do not pop and each new title is overlaid atop the old one.
I set the titles for each of the view controllers in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = #"More";
}
When I don't attempt the pop to root followed by the delay followed by the push - the titles and views behave as expected with no overlapping occurring.
Example Images from screenshots
I've had a good dig around stack overflow but I can't see any questions which describe the same issue as the one I am having.
Qn.1: Is there something fundamentally incorrect with the popToRoot, Delay, push View approach?
Qn.2: If anyone out there has seen this kind of behaviour before, how did you resolve it?
Increasing the delay from 0.1f to 0.5f fixed the problem
Change
[[NSNotificationCenter defaultCenter] performSelector:#selector(postNotificationName:object:) withObject:kMessages afterDelay:0.1f];
to
[[NSNotificationCenter defaultCenter] performSelector:#selector(postNotificationName:object:) withObject:kMessages afterDelay:0.5f];

How to push next view with TTNavigator?

I'm using a modified View-Based Application, where I have starting UIViewController showing a input control & a TTThumbsViewController showing the results. I'm passing them in the AppDelegate using TTNavigator initWithName: for the TTThumbsViewController.
I did this by myself reading from the documentation:
The ViewDidLoad method inside my TTThumbsViewController:
- (void)viewDidLoad {
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:myTitle
photos:myImages
photos2:nil
];
}
The implementation of my AppDelegate:
#implementation geoFlickrAppDelegate
#synthesize window=_window;
#synthesize viewController=_viewController;
#synthesize navigator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigator =[TTNavigator navigator];
navigator.window = self.window;
TTURLMap *map = navigator.URLMap;
[map from:#"app://home/" toViewController:[geoFlickrViewController class]];
[map from:#"app://album/(initWithName:)" toViewController:[AlbumController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"app://home/"]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
-(void)toGallery:(NSString*)txt
{
[navigator openURLAction:[TTURLAction actionWithURLPath:txt]];
}
The event inside my UIViewController for pushing the next view:
-(IBAction)search:(id)sender
{
NSString *str = [[NSString alloc] initWithFormat:#"app://album/%#",txtSearch.text];
geoFlickrAppDelegate *appDelegate = (geoFlickrAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate toGallery:str];
}
The result from this code is that the input is passed through my AppDelegate in the TTThumbsViewController using initWithName: but the never gets pushed in & my ViewDidLoad method never gets called. Any idea why is that so?
Whenever I had an error like this, it was the initializer (in your case initWithName:) returning nil. This is the first thing I would check. If that doesn't solve try setting a breakpoint in [TTBaseNavigator presentController:parentURLPath:withPattern:action:].
If that method is not reached something is wrong with your URL-Map. You may for Example need to urlencode your users input. URL-based Navigation works with URLs. Strings that can not be used in URLs can not be passed unencoded.
If that method is reached, you may use the debugger and step through the code from there to find out whats wrong.
Another thing I would like to mention, is that you really do not need to reference your appDelegate with [[UIApplication sharedApplication] delegate] when you like to use the navigator. This is one of the reasons why the navigator is so useful. Try to change the call to:
-(IBAction)search:(id)sender
{
NSString *str = [[NSString alloc] initWithFormat:#"app://album/%#",txtSearch.text];
TTOpenURLFromView(str, self.view);
TTOpenURL(str); // Three20 < 1.0.3
}
You can then get rid of the toGallery: method.
In my code I have no:
navigator.window = self.window;
and instantiate the window calling:
navigator =[TTNavigator navigator];
navigator.window;
and also change [self.window makeKeyAndVisible] into
[navigator.window makeKeyAndVisible]