Open ViewController from different class - objective-c

I want to be able to open a ViewController from a different class. So I could simply call it to open a view wherever I need it.
So I have this setup in the class that holds the code:
+ (void)openCalcView: (NSString *)nameOfView {
UIViewController *controller;
if ([nameOfView isEqualToString:#"Tax"]) {
controller = [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
}else if ([nameOfView isEqualToString:#"Rent"]){
controller = [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
}
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}
But [self presentViewController:controller animated:YES completion:nil]; gives me a warning:
Class method '+presentViewController:animated:completion:' not found (return type defaults to 'id')
I can call simple things like NSLog through this, from any class. But this doesn't work.

Because openCalcView is a Class method, there is no UIViewController instance (i.e. no [self presentViewController:] method).
You'll need to also pass a UIViewController to this class method, something like this:
+ (void)openCalcView: (NSString *)nameOfView fromViewController:(UIViewController *)controller {
UIViewController *newController;
if ([nameOfView isEqualToString:#"Tax"]) {
newController= [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
}else if ([nameOfView isEqualToString:#"Rent"]){
newController= [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
}
newController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[controller presentViewController:newController animated:YES completion:nil];
[newController release];
}
the controller parameter represents the UIViewController that is opening the new view controller

Related

xcode 9: presentViewController removing all previous controller

I have view controller and implement like this :
loginViewController = [[LoginViewController alloc] init];
loginViewController.delegate = self;
[self.view addSubview:loginViewController.view];
[self addChildViewController:loginViewController];
[loginViewController didMoveToParentViewController:self];
Inside loginViewController, I want to show another view controller but I implement like this :
otherViewController = [[OtherViewController alloc] initWithNibName:nil bundle:nil];
otherViewController.delegate = self;
[self presentViewController:otherViewController animated:YES completion:nil];
Everytime I done with otherViewController and want to dismiss the view controller [self dismissViewControllerAnimated:YES completion:nil];
it restarted from the beginning and not showing the loginViewController. I tried to check using Debug View Hierarchy and got that no loginViewController is implemented when I called presentViewController:otherViewController
Why is this happened ?

Objective-C: How to make a button take you to another class/storyboard

I want to make the bottom button seen in the storyboard on the right to take the user to the signupstoryboard.storyboard/signupController seen on the left when the user taps on the button. How can that be done?
{ - (IBAction)signupbutton:(id)sender; SignupController *signUpView = [self.storyboard instantiateViewControllerWithIdentifier:#"signup"]; [self presentViewController:signUpView animated:YES completion:nil]; }
You'll have to do it by code.
- (IBAction)buttonTapped
{
NSString *storyboardName = #"signupstoryboard";
UIViewController *nextViewController = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateInitialViewController];
// if you are using a navigation controller to push it:
[self.navigationController pushViewController:nextViewController animated:YES];
// otherwise if you are presenting it modally:
[self presentViewController:nextViewController animated:YES completion:nil]
}
In your .m (at the top)
#import "signupController.h"
In your button action method (Don't forget to set the storyboard id for your signupController as signup
- (IBAction)signupbutton:(id)sender
{
SignupController *signUpView = [self.storyboard instantiateViewControllerWithIdentifier:#"signup"]; [self presentViewController:signUpView animated:YES completion:nil];
}
Thats it

How to present a SLCompose view controller from NSObject Class

Here is my code :
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *viewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social platform to use it, e.g. facebook or twitter
[viewController setInitialText:sQuotes];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navController presentViewController:viewController animated:YES completion:nil];
[viewController setCompletionHandler:^(SLComposeViewControllerResult result)
{
NSString *output;
switch (result)
{
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
{
output = #"Post Successfull";
}
break;
default:
break;
}
[appDelegate.navController dismissViewControllerAnimated:YES completion:nil];
}];
}
But it Shows the warning
on whose view is not in the window hierarchy!
The warning is pretty clear. You are trying to present a modal view by a view that is not in the window hierarchy, which won't work.
Try changing this [appDelegate.navController presentViewController:viewController animated:YES completion:nil];
to something like this instead:
[appDelegate.window.rootViewController presentViewController:viewController animated:YES completion:nil];
This guarantees that the currently active root view controller of your app will be the one presenting the modal view controller.
Depending on how your AppDelegate is built you may need to add a property or just a getter to surface the window variable to the outside world.

Use code across multiple class files

Let's say I have this code:
if ([resultButton.titleLabel.text isEqualToString:#"Tax"]) {
TAXViewController *controller = [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}else if ([resultButton.titleLabel.text isEqualToString:#""]){
RENTViewController *controller = [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}else if //repeats a lot......
And I want to use this same exact code in multiple .h files. Is there a way to write the code (in only one place) and access it from other .h files without having it repeat everywhere?
So in the end I guess, I could just pass a string to that other .h/.m files and it would open the view with the matching string.
Put it in one class and make all of the other files you want to access it in a subclass of it.
#interface SomeController : BaseViewController
You can also WAY simplify your code
UIViewController *controller;
if ([resultButton.titleLabel.text isEqualToString:#"Tax"])
controller = [[TAXViewController alloc]initWithNibName:#"TAXViewController" bundle:nil];
else if ([resultButton.titleLabel.text isEqualToString:#""])
controller = [[RENTViewController alloc]initWithNibName:#"RENTViewController" bundle:nil];
else if
....
//after all if-else statements
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];

viewWillAppear is not called

I know that viewWillAppear is not called on pop/push views, but I really need that method. Here is what I try
I added UINavigationControllerDelegate and adopt
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear
{
NSLog(#"Log");
}
but viewWillAppear is still not invoked
EDIT
AppDelegate.m
self.navigationController = [[UINavigationController alloc]init];
[self.window setRootViewController:self.navigationController];
FirstView *fview = [FirstView]alloc]init];
[self.viewController pushViewController:fview animated:YES];
FirstView.m
....
-(void)viewWillAppear
{
NSLog(#"Logged");
}
....
The clue is here:
[viewController viewWillAppear:animated];
}
-(void)viewWillAppear
You call a method that takes one parameter. But your method doesn't have one. In Objective C terms that's a completely different method.
It should look like this:
-(void)viewWillAppear:(BOOL)animated {
// blah
}
do you have navigation controller on window?
paste your appdelegate.m
my working code:
self.navController = [[[CustomNavigationController alloc] initWithRootViewController:[[[HomeViewController alloc] init] autorelease]] autorelease];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];