Use code across multiple class files - objective-c

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];

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 ?

Present UIImagePicker inside Modal View

I pushed modally to a new view controller using:
[self presentViewController:picker];
and then from within that new view controller I did;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
But the camera won't even show, why?
I had the same problem on iPad and below code worked for me...
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//present your view controller here...
}];
Good Luck !! :)
This might be due to couple of reasons:
1) Create a method of Camera Opening code:
-(void) openCamera {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil]; }
And call it like this Wherever you want,
[self performSelector:#selector(openCamera) withObject:nil afterDelay:0.1];
OR
2) Write code inside this method,
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//... Camera Code ....
}];

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

Open ViewController from different class

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

How to dismiss and present the same modal view controller right after?

I want to make it seem as if it was a reset button.
It would take me to my mainviewcontroller screen and then come right back.
MainViewController.m
MainViewController.h
MainViewController.xib
ViewController.xib
ViewController.m
ViewController.h
I do not understand how I am supposed to do this.....
- (IBAction)newGame:(id)sender {
[self dismissModalViewControllerAnimated:YES];
ViewController *screen = [[ViewController alloc] inittWithNibName:#"ViewController" bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
Any ideas of how to do this?
Thanks
Do that without animation, as following.
- (IBAction)newGame:(id)sender {
[self dismissModalViewControllerAnimated:NO];
ViewController *screen = [[ViewController alloc] inittWithNibName:#"ViewController" bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:NO];
}