Check if controller has already been loaded XCode - objective-c

I have the following code to push a new ViewController in a Split View Controller:
Level4ViewController *controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
The only problem I have, if I were to run this again, a new controller will show, I would like to be able to go to the view that I had before with all my data.
Can anyone help me out here.
Thanks.
EDIT:
Updated code?
Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
controller = view;
if(controller == nil)
{
controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
}
else {
controller = [self.navigationController.viewControllers objectAtIndex:1];
}
}
}
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;

UINavigationController has a property viewControllers which is a NSArray that hold all the stack that has been pushed to the navigation controller, in this array you can check for your view controller if it is there use that one - you check like this -
Level4ViewController *lvc;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
lvc = view;
}
}
and if you already knows that at which index your viewcontroller is there then you can get it from that index as -
Level4ViewController *lvc = [self.navigationController.viewControllers objectAtIndex:1];
update -
Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
controller = view;
}
}
if(controller == nil)
{
controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
}
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;

If you are using a navigation controller
FirstScreenViewController *firstScreenVC = [self.storyboard instantiateViewControllerWithIdentifier:#"1S"];
if (![self.navigationController.topViewController isKindOfClass:FirstScreenViewController.class])
[self.navigationController pushViewController:firstScreenVC animated:YES];

Related

Heading UITableView issue in ios8

I am using splitviewcontroller into my app.depends on selection in master view.......deatil view will appear........we are having multiple detail view.
This my code to call the detail view
- (void)setDetailItem:(id)newDetailItem
{
[self.navigationController setDelegate:self];
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
}
if([[self.detailItem description] isEqualToString:#"Companies"])
{
listViewController_ = [self.storyboard instantiateViewControllerWithIdentifier:#"ListStoryBoard"];
[self addChildViewController:listViewController_];
if (![[PSAWebServices sharedInstance] isPushInProcess]) {
[self.navigationController pushViewController:listViewController_ animated:YES];
}
}
else if ([[self.detailItem description] isEqualToString:#"Contacts"])
{
contactlistViewController_ = [self.storyboard instantiateViewControllerWithIdentifier:#"contactListView"];
[self addChildViewController:contactlistViewController_];
if (![[PSAWebServices sharedInstance] isPushInProcess])
{
[self.navigationController pushViewController:contactlistViewController_ animated:YES];
}
}
else if ([[self.detailItem description] isEqualToString:#"Projects"])
{
projectViewController_ = [self.storyboard instantiateViewControllerWithIdentifier:#"projectListView"];
[self addChildViewController:projectViewController_];
if (![[PSAWebServices sharedInstance] isPushInProcess])
{
[self.navigationController pushViewController:projectViewController_ animated:YES];
}
}
else if ([[self.detailItem description] isEqualToString:#"Activities"])
{
activitiesViewController_ = [self.storyboard instantiateViewControllerWithIdentifier:#"activityListView"];
[self addChildViewController:activitiesViewController_];
if (![[PSAWebServices sharedInstance] isPushInProcess])
{
[self.navigationController pushViewController:activitiesViewController_ animated:YES];
}
}
else if ([[self.detailItem description] isEqualToString:#"Calendar"])
{
calendarViewController_ = [self.storyboard instantiateViewControllerWithIdentifier:#"CalendarStoryBoard"];
//[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:calendarViewController_];
//[self.view addSubview:self.calendarViewController.view];
//[self.navigationController pushViewController:calendarViewController_ animated:YES];
if (![[PSAWebServices sharedInstance] isPushInProcess])
{
[self.navigationController pushViewController:calendarViewController_ animated:YES];
}
}
}
here is problem which come only first time
My question is I am not able to set the frame of the detail view in ios8 with using the Auto layout and constraints
can u plz help me out this?
Thanks in advance
but i when rotated device it get fit correctly in screen
where i am getting wrong

navigation issue between viewcontrollers

After pushing a viewcontroller, when I want to pop it back my application is crashing.
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count>0) {
[[self navigationController] popViewControllerAnimated:YES];
}
the controller I'm pushing, has uiwebview and for the requirement I m doing
[twebView stopLoading];
I'm clueless. anyone please give me a good working suggestion.
and is there any easy way to pushviewcontroller from right to left??
First alloc your array first. Refer following code:
NSArray *viewControllers = [[NSArray alloc]init];
viewControllers = self.navigationController.viewControllers;
NSLog(#"Controllers = %#",viewControllers);
if (viewControllers.count>0) {
[[self navigationController] popViewControllerAnimated:YES];
}
And also check log of controllers as above.
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[OldViewController class]])
{
//Do not forget to import AnOldViewController.h
[self.navigationController popToViewController:controller animated:YES];
break;
}
}

How do I switch a new view controller in iOS development?

I am trying to write an IBAction to switch view controllers for my iPhone application:
-(IBAction)changeToView2:(id)sender
{
if (self.view2 == nil)
{
view2 = [[UIViewController alloc] initWithNibName:#"View2Controller" bundle:[NSBundle mainBundle]];
}
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentedViewController: view2 animated:YES];
}
However, I am getting a build error noting that no interface declares "presentedViewController:animated:". Why?
Changing this to "presentViewController:animated:" produces the same error.
The method is presentViewController:animated:completion:, not presentedViewController:animated:
Instead of
[self presentedViewController: view2 animated:YES];
try
[self presentViewController: view2 animated:YES];
[self presentViewController:view2 animated:YES];
Try this your edited code.I think this will be helpful for you.
-(IBAction)changeToView2:(id)sender
{
if (self.view2 == nil)
{
view2 = [[UIViewController alloc] initWithNibName:#"View2Controller" bundle:nil];
}
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentedViewController: view2 animated:YES];
}

How to use a UIButton to Change Views

How can I changed the view in iPhone development using an UIButton. I do not want to use a toolbar. Thank you
You must have a root view controller
This root view controller switches between the two views.
Something like this:
- (IBAction)switchViews:(id)sender {
if (self.vc1 == nil) {
self.vc1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.vc2 removeFromSuperview];
[self.view addSubView:vc1];
vc2 = nil;
}
else {
self.vc2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.vc1 removeFromSuperView];
[self.view addSubView:vc2];
vc1 = nil;
}
}

Is this the best way to dynamically load a UIViewController object based on item selection?

My root view contains three sections in a table view. Depending on which section is selected a corresponding view controller will be popped onto the view stack. The following didSelectRowAtIndexPath method is from my code and it works as I expect, but I was wondering if this is the correct/most elegant way to do it. I'm an Objective-C newcomer so I'm not sure if I should be intitializing the viewController to nil first.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *viewController = nil;
if (indexPath.section == 0) {
viewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
}
else if (indexPath.section == 1) {
viewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}
else if (indexPath.section == 2) {
viewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
}
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
You could do this in a swtch() statement, but otherwise, I think this is good. The only other change would be to check for a viewController before pushing:
if (viewController != nil) [self.navigationController pushViewController...