Switch Active View Controller Programmatically - objective-c

I have 2 views as part of my Navigation Controller.
I created a class for my Navigation Controller with a public method that certain actions on the first view will trigger to load the Detail View Controller. I am importing the Detail View Controller, but nothing in my code is working. I know the method is being called correctly because I'm logging it.
What am I doing wrong here?
#import <UIKit/UIKit.h>
#interface NearbyController : UINavigationController
- (void) nextpage;
#end
#import "NearbyController.h"
#import "DetailView.h"
#implementation NearbyController
-(void) nextpage {
NSLog(#"working");
DetailView *nextView = [[DetailView alloc] init];
[self pushViewController:nextView animated:YES];
}
#end

If you initialize a view controller programmatically you should use this init.

If you have set a storyboard ID you can do this
DetailView *nextView = (DetailView *)[self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewControllerID"];
[self pushViewController:nextView animated:YES];
If you want to load from a XIB you can call
DetailView *nextView = [[DetailView alloc] initWithNibName:#"MyDetailViewControllerID" bundle:nil];
[self pushViewController:nextView animated:YES];
Hope it helps!

If you're using storyboards what you wanna do is setup the storyboard id in the identity inspector on your DetailViewViewController
Then you would wanna instantiate your view controller like so:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DetailView *nextView =[sb instantiateViewControllerWithIdentifier:#"DetailViewViewController"];
/* your identifier is your storyboard id*/
sb = nextView.storyboard;
Then you should be able to push your view controller.
Im still new to this but i hope this helps!

Related

Navigate to storyboard from a viewController programmatically

in my iOS application, I would like to navigate programmatically to a whole storyboard (tabBarController) from a viewcontroller.
I found this solution:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"SMShopViewController"];
[self.navigationController pushViewController:controller animated:YES];
but it is not what I need, I want to show directly my tabBarController
thank you for your reply
As i understand, You have UITabBarController on storyboard like this and you want show it from code?
So, Firstly set storyboard identifier for UITabBarController
Then in code
UITabBarController *tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"KITTabBarController"];
[self.navigationController pushViewController:tabBar animated:NO];
If you have parent navigation controller better way to do it is:
[self.navigationController setViewControllers:#[tabBar] animated:YES];

How to pass an uiimage to another view controller?

How to pass the uiimage selected in uiimagepickercontroller in MainViewController to SecondViewController? It did push to the SecondViewController, however the uiimage is empty.
I've searched through the web, tried with the solutions but still cannot get it work.
photoImage is the uiimageview I've declared in SecondViewController.
- (void)imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[secondVC.photoImage setImage:[info objectForKey:#"UIImagePickerControllerEditedImage"]];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
A UIImageView in your second view controller doesn't exist until the viewDidLoad method of that controller is called. Put a property into SecondViewController that holds a UIImage, store the reference to the image you want to use in it when you push the view controller (instead of trying to set the image view), and then move your setImage: call into the second view controller's `viewDidLoad``.
Create a shared class that will store the image, and access it wherever you want.
In MainViewController assign that image in that shared class' ivar, and in SecondViewController access it.
Edit :
Below code shows how to implement shared class, this may not be syntactically correct, because currently I am not using mac, so I cant complile.
#interface SomeManager : NSObject
+(id)myImage;
#end
#implementation SomeManager
+(id)myImage{
static id myImage= nil;
#synchronized([self class]){
if (myImage== nil) {
myImage= //put your image here;
}
}
return myImage;
}
#end

Jump between different viewControllers using UIButton?

I have a button that open another viewController(familyView) when clicked.
In familyView there is another button which suppose to bring me back to the mainViewController(ViewController.xib) but I don't know how to call the main viewController.
My method to call familyView
UIViewController* familyView = [[UIViewController alloc] initWithNibName:#"familyView" bundle:[NSBundle mainBundle]];
[self.view addSubview:familyView.view];
I hope you could help on how to call the main ViewController ? do I have to use the same method to call it? like this I mean:
UIViewController* mainView = [[UIViewController alloc] initWithNibName:#"viewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:mainView.view];
If yes, is there a better way to implement this? in my demo project, I'm trying to make 7 views full with data and a button to go back and forth.
EDIT:
If I use UIView would that be best for me instead of using different viewControllers with their implementations and interfaces files?
My project will have views, and each view has data on it parsed from a different html page.
There are two method that can be used.
UINavigationController
Delegates
From your question it seems that a UINavigationController is the best option but I will show you both.
UINavigationController
When you load your mainViewController from your app delegate your going to need to wrap it in a nav controller like so:
AppDelegate.h
#property (strong, nonatomic) UINavigationController *navController;
AppDelegate.m
#synthesize navController = _navController;
//in didFinishLaunchingWithOptions:
UIViewController *mainViewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
navController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.window.rootViewController = nav1;
[self.window makeKeyAndVisible];
Now in your MainViewController you have the convince of UINavigationController.
When you want to push to a child from a parent you can simply do:
ChildViewController *child = [[ChildViewController alloc]...];
[self.navigationController pushViewController:child animated:YES];
If you in your ChildViewController and want to go back simply do:
[self.navigationController popViewControllerAnimated:YES];
This is the "Drill Down" technique.
(I know "Drill Down" has more meaning than simply that but it provides a good frame of reference.)
Delegate
Now the other method that you have is to setup delegates between the classes. So if your in childView and need to call your parent, you will have a channel to do so.
In your MainViewController.h setup it like so:
#import <UIKit/UIKit.h>
//This is our delegate
#protocol TalkToParentDelegate <NSObject>
//This is our delegate method
- (void)helloParent;
#end
#interface MainViewController : UIViewController <TalkToParentDelegate>
...
..
#end
In your MainViewController.m make sure add the delegate method.
- (void)helloParent {
NSLog(#"Hello child, let me do something here");
}
In your ChildViewController.h setup it like so:
#import <UIKit/UIKit.h>
//Add header of class where protocol was defined
#import "MainViewController.h"
#interface ChildViewController : UIViewController
//Create a property we can set to reference back to our parent
#property (strong, nonatomic) id <TalkToParentDelegate> delegate;
#end
Now, in your MainViewController.m , whenever you present your ChildViewController do this:
ChildViewController *child = [[ChildViewController alloc]...];
//Set the delegate reference to parent
child.delegate = self;
//present the view
Last but not least, no when you in your child you can call methods on your parent (MainViewController) like so:
[self.delegate helloParent];
So here are two methods that you can use.
I would like to note however, you can use these together. Say you had a UINavigationController but still needed a child to talk to its parent, you can setup a delegate so that's possible.
Hope this helps.

Pushing a controller from a subview

In essence what is the correct way of pushing a new view controller from a sub view of a navigation controller.
The issue being subviews of the navigation view don't inherit the self.navigationController (its nil)
The reason is I need separate controllers for the navigation bar view & the main view but both need to push new controllers.
I am willing to change this model if someone can tell me the correct way of doing this.
Also:
AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate
[del.navigationController pushViewController:vc animated:YES];
Does not work as the delegates controller is nil.
Create a following category on UIView.
#interface UIView (GetUIViewController)
- (UIViewController *)viewController;
#end
#implementation UIView (GetUIViewController)
- (UIViewController *)viewController;
{
id nextResponder = [self nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return nextResponder;
} else {
return nil;
}
}
#end
Now get the SuperView from the subView.
mySuperView = [mySubView superview];
Then call the method from category created.
mySuperViewController = [mySuperView viewController];
Now, using this viewController, you can access the navigationController.
I have not tried the above code and approach, but I hope it should work.
You may try two ways
1.Use the superViewController's navigationController to push your viewController
2.Embed your current viewController in a NavigationController so that the navigationController won't be nil

Cocoa - Adding a Navigation Controller to a subview

I'm currently working on a view based app for the iPad that has 3 seperate views on the main page. A custom menu up the top, a status list on the side, and a main view. The issue I am having with the main view is trying to add a navigation controller.
In AppPadViewController.h
#interface AppPadViewController.h : UIViewController {
MainViewController *MainView;
}
#property (nonatomic,retain) IBOutlet MainViewController *MainView;
And in AppPadViewController.m
#synthesize MainView;
- (void) viewDidLoad {
[super viewDidLoad];
MainView.navigationItem.title = #"Home";
UINavigationController *mainNavController = [[UINavigationController alloc] initWithNibName:#"MainView" bundle:[NSBundle mainBundle]];
self.MainView = [MainViewController alloc] initWithRootViewController:mainNavController];
}
And in the nib I have added the view where I would like it, and tied it in to the MainView, and then added the MainViewController and tied it to the File Owner and view.
When I run this, I get an 'Unrecognized Selector" error thrown on the initWithRootViewController line.
Can anyone see any problem with the code, or suggest a better way to add a navigation controller to a sub view?
You have your two view controllers reversed. Try something like this:
self.MainView = [[MainViewController alloc] initWithNibName:#"MainView" bundle:[NSBundle mainBundle]];
UINavigationController *mainNavController = [[UINavigationController alloc] initWithRootViewController:MainView];