Changing ViewController from an UIImage - ios

I'm trying to change the ViewController when an area in a pic is pressed.
I tried doing this :
-(void)openDrawer{
UIStoryboard *mStoryBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc =[mStoryBoard instantiateViewControllerWithIdentifier:#"DrawViewController"];
[self.navigationController pushViewController:vc animated:YES];
}
but self.navigationController does not exist, that is understandable, but I can not find a way to get a the current viewController with in the Image
I tried putting it in a different class, delegating from the viewController of the current view.
-(void) changeChangeViewToName:(NSString *)name{
UIStoryboard *mStoryBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc =[mStoryBoard instantiateViewControllerWithIdentifier:name];
[self.navigationController pushViewController:vc animated:YES];
}
This works from inside the viewController but I can't call the function from inside the Image.

Post a notification from the class (image?) that decides it wants the new view controller displayed. Listen for that notification in the existing view controller that is capable of doing the push.

Phillip Mills is right. You could also use the delegate pattern:
Create a protocol for objects that should be able to perform the desired action:
#protocol YourImageDelegate
-(void)touchUpInsideArea;
#end
Add this method to your view controller:
-(void)touchUpInsideArea {
[self openDrawer];
}
Set up your view controller to conform to the protocol:
#interface YourViewController : UIViewController
And don't forget to include your protocol in both files: the view controller class and the image class.

Related

How to open new view controller after click button?

I added one ViewController to my project and created one class.
After I bind this class to my ViewController.
In another controller I have method:
- (IBAction)login:(id)sender {
// How here I can do redirect to controllerViewController
}
There are two ways to push view controllers in the application.
1) by Segue
2) By View controller identifier
1) By Segue :
[self performSegueWithIdentifier:"SegueIdentifier" sender:self];
2) By View controller identifier :
Yourclassname *gotoYourClass = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerIdentifier"];
[self.navigationController pushViewController:gotoYourClass animated:YES];
- (IBAction)login:(id)sender {
ViewController *vc = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil]; }
In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code, to bring that view forward.
IF YOU WANT TO USE PUSH THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self.navigationController pushViewController:vc animeted:YES];
IF YOU WANT TO PRESENT THEN USE THIS CODE
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
Note: In UIViewController please enter your view controller name which you want to push into another view.
If you want to comeback to the current ViewController later then use Navigation ViewController or else just use the presentedViewController of self(your current viewController)- no going back business, like they have previously illustrated.
For a simple block of execution(demo or practice) it's all the same but for a project application it completely matters to make the correct choice.

Perform segue or presentviewcontroller from custom uiview

I looked at many stackoverflow answers and tried most of them. May be I am doing something wrong but if I can get clarified, it will be wonderful.
My viewcontroller LoginViewController has a method which gets called and custom alertview which is a uiview is shown to user. After user enters 4 digit pin, I need to call another view controller from that custom alertview (uiview).
My current setup is:
LoginViewController.h:
#import "LoginPinAlertView.h"
#interface LoginViewController : UIViewController <LoginPinAlertViewDelegate,... > {
LoginPinAlertView *customAlertView;
}
LoginViewController.m:
-(void) viewDidLoad {
...
customAlertView.myLoginVC = self;
}
Inside my UI VIew (which a customer alert view)
LoginPinAlertView.h
#class LoginViewController
...
#property (nonatomic, retain) LoginViewController *myLoginVC;
LoginPinAlertView.m
....
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DestinationViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MyDestViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self.myLoginVC presentViewController:vc animated:NO completion:NULL];
}
...
But this is not working right now. What my requirement is, from UI View (which is not part of any contorller because this is custom alert view which many controllers are using), I wish to push/present another view controller.
hope this will help you ,
you cannot push/present view controller from a uiview class for it. you should check out this answer , https://stackoverflow.com/a/26011073/3767017

iOS: Call segue from coded UIViewController outside of Storyboard

I have a custom UIVieController class that it's not included in any StoryBoard, I need to call from this class an UIViewController that is included in a StoryBoard, I tried many ways to do this but it doesn't work, always get a NSException because when I try to instance the UIViewController of the StoryBoard and get a nil object.
I don't know why it's happening this, I really appreciate any help with this.
Thank you.
Just to clarify in this code I omitted the pushing of the view to the UINavigationController.
I tried this.
+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
AXISWebViewViewController *axisWebView = [storyboard instantiateViewControllerWithIdentifier:segueId];
}
and this
+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{
AXISWebViewViewController *axisWebView = [delegate.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:segueId];
}
Ok the first thing you need to do is to add a identifier to you view controller at Storyboard, you can do that opening the Storyboard's Identity Inspector and fill the storyboard id field.
Now you are able to call this UIViewController from any place you want like this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"the name of your storyboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"the id of your view controller"];
I hope this can help you.
Please post the code you are using to instantiate the VC from the Storyboard.
In general:
To instantiate a View Controller from StoryBoard:
MyViewController *MyVC = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
Make sure you set the Storyboard ID and Restoration ID and Class name to "MyViewController" or whatever you want to call it and use that as the parameter for instantiateViewControllerWithIdentifier:
Then if you are in a navigation controller, simply push the instantiated view controller onto the navigation stack:
[self.navigationController pushViewController:MyVC animated:YES];
or present it modally:
[self presentViewController:MyVC animated:YES completion:nil];

How do I segue from a NIB subview to a viewcontroller in the storyboard

I have a combination of a storyboard and NIB viewcontrollers how can I segue from one to the other
This is the Scenario:
UITabBarController(storyboard)|- UINavigationController (subclassed in storyboard) -> [UIViewController (NIB)] -> UIViewController (in Storyboard)
You'll have to give the UIViewController in the Storyboard a 'Storyboard ID' (in the Identity inspector tab). Then instantiate that ViewController in your NIB ViewController's code like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"YourViewControllerId"];
[self.navigationController pushViewController:vc animated:YES];
In your UINavigationController subclass you need to instantiate your view controller with its view in a NIB(let's call it NIBViewController) and set that view controller as the rootViewController with the method setViewControllers:animated:. Do it in awakeFromNib like this:
- (void)awakeFromNib
{
[super awakeFromNib];
NIBViewController *viewController = [[NIBViewController] init];// It will look for the appropriate XIB as long as you did not change its name and it's in the main bundle, otherwise use initWithNibName:bundle:
[self setViewControllers:#[viewController] animated:NO];
}
Then, to go from the NIBViewController to other view controller contained in the storyboard just set an identifier in the storyboard for that view controller, instantiate it and push it or present it, as you want.
It is important to mark that in your NIBViewController instance self.storyboard will be nil because that view controller is not contained in a storyboard, so you need to instantiate a new storyboard(solution A**) or get the storyboard from the navigation controller(solution B):
// Solution A
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// Solution B
UIStoryboard *storyboard = [self.navigationController storyboard];
UIViewController *storyboardViewController = [storyboard instantiateViewControllerWithIdentifier:#"viewControllerId"];
[self.navigationController pushViewController:storyboardViewController animated:YES];
** Please note that this is creating a new instance of the storyboard, so better not to try tricky things and avoid segues to previous view controllers contained in the storyboard or you will find that those view controllers are not the same ones... It's the first solution that came to my mind, but definitely you should go for the solution B.

Linking a new viewcontroller to Storyboard?

There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

Resources