Change to another View Controller when pressing cell of UITableView - objective-c

Hi I have a uitableview on my view controller and I want to go to another view controller when a cell is pressed, I read a couple of tutorial but all where from a table view controller.
Here is the code I have, it doesn't do anything when I pressed the cell.
EDIT: I decided to create a custom button on the cell and from there go to the detail view
Here is what I put on the button, I get a thread 1: breakpoint 1.1 error on the second line:
- (IBAction)buttonSelected3:(id)sender {
UIStoryboard *storyBoard; storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
InfoFarmaciaViewController *vc = (InfoFarmaciaViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"InfoFarmaciaViewController"];
[self.navigationController pushViewController:vc animated:NO];}

You don't have to release the variable, ARC will take care of that. Just remove that line? And don't set it to null. Just push it, then you are done.

Are you using storyboard for that? - i assume not, change your views to storyboard and do the following:
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
CustomViewController *vc = (CustomViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"idofthestoryboardview"];
[self.navigationController pushViewController:vid.fileViewController animated:YES];

Related

viewcontroller with a uinvaigationcontroller when pop up does not show the navbar

I'm using storyboards with this. On the storyboard I have a "mainpage" uiviewcontroller that has a button which when tap will call another uiviewcontroller (choosewinner) to pop up. But this choosewinner vc has a uinavigation controller attached to it, reason why is so that it can go back and forth easily. Now I can call the uiview as a modal pop up but it doesn't show it's uinavigation.
This is the method called when the button is tapped. Thoughts?
- (void)updateWinner{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MatchWinnerViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"WinnerViewController"];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:vc animated:NO completion:nil];
vc.view.superview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
vc.view.superview.bounds = CGRectMake(0, 0, 800, 600);
}
Even though you have the MatchWinnerViewController with UINavigationController attached to it in the storyboard, if you want to present it modally with the UINavigationController, you need to supply the navigation controller as the parameter to the method instantiateViewControllerWithIdentifier.
The reason is that for modal presentation, it takes the root view controller, and a navigation controller is also a view controller.

Changing View Controllers - SIGABRT

I am trying to move from one view controller to another when a button is pressed, but it is giving me "sigabrt" error.
This is my code:
-(IBAction)buttonPressed:(id)sender{
StatsViewController *myStats = [self.storyboard instantiateViewControllerWithIdentifier:#"StatsViewController"];
[self presentViewController:myStats animated:YES completion:nil];
}
The only reason i am doing it this way is because i need to pass data forward from one view controller to the next.
I also checked my storyboard there are no connections between the two view controllers (just the above code).
However, when i remove the above code and connect the two view controller using the "control drag" method on the storyboard, it works. no error..
Make sure that the storyboard instance is not nil and and the Storyboard Id is set properly
-(IBAction)buttonPressed:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
StatsViewController *lvc = [storyboard instantiateViewControllerWithIdentifier:#"StatsViewController"];
[self.navigationController pushViewController:lvc animated:YES];
}

Storyboards: Open UIViewController from storyboard as popover from image in UITableViewCell

I have a Custom UITableViewCell class and the custom cell has an image button in it that is linked back to a method on the cell class. When this method is triggered, I want to launch an orphaned UIViewController from the storyboard inside a popover. I've tried several techniques for doing this. Interface Builder will not compile if I add the UIViewController as a segue from the button on the prototype cell. Does anyone have any suggestions?
UPDATE: I Got it working with the following:
UITableView *tv = (UITableView *) self.superview;
UITableViewController *vc = (UITableViewController *) tv.dataSource;
UIStoryboard *storyboard = vc.storyboard;
UIViewController *actionView = [storyboard
instantiateViewControllerWithIdentifier:#"ActionView"];
popoverController = [[UIPopoverController alloc]
initWithContentViewController:actionView];
popoverController.popoverContentSize = CGSizeMake(320, 416);
[popoverController presentPopoverFromRect:self.actionButton.bounds
inView:self.actionButton
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You should be able to use instantiateViewControllerWithIdentifier: from UIStoryboard to pick up any view controller if you have it identified correctly. (Since the easiest way to get a reference to the storyboard is from some view controller that already came from it, you may want to have the cell notify the current controller to do this...that depends on how your objects are connected.)

How do I implement a UINavigationController in this case?

current version of my project :
I have 5 different UIViewControllers in my app. I've set my
FirstViewController to be the Initial View Controller using the
Attributes Inspector. I move back and forth from one ViewController to
another by using buttons to which I assign modal segues, from one
ViewController to another, using the StoryBoard
What I want to change:
I want to keep the navigation buttons obviously, delete the modal segues and use
a UINavigationController instead. If I understand the concept
correctly, when using a UINavigationController I need to go into each
UIButton-IBAction and at the very end of the method I have to push the next
ViewController I want to move to, onto my NavigationController (do I also
have to pop the current one first?). However, I can't figure out how
to implement all that correctly.
What I've done so far:
I removed all modal segues from the storyboard and kept the navigation buttons along with their corresponding IBActions
I unchecked the box in the Attributes Inspector that was making my FirstViewController the initial View Controller of my app
I went into my AppDelegate.m and tried to create the Navigation Controller there and make my FirstViewController be the RootViewController
MyAppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *myFirstViewController = [[FirstViewController alloc] init];
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
[myNavigationController pushViewController:myFirstViewController animated:YES];
// Override point for customization after application launch.
return YES;
}
I then tried to test if the above was working by going into the IBAction of a
navigation button on my FirstViewController and implemented the
following in order to move to my SecondViewController when the
button is pressed :
FirstViewController.m
- (IBAction)goRightButton:(UIButton *)sender
{
// some code drawing the ButtonIsPressed UIImageView on the current View Controller
UIViewController *mySecondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:mySecondViewController animated:YES];
}
but nothing happens. What am I doing wrong ?
You are not linking your XIB file. Please add your navigation controller as
UIViewController *myFirstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
Use following code to move from one view to another
UIViewController *mySecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:mySecondViewController animated:YES];
If you are using a storyboard, you should just drag in the navigation controller there and hook it up to your app delegates. As long as it is the main storyboard, and you have identified a view controller to load first, you do not need to load any views in your app delegate.
In order to push a view programmatically that's in a storyboard, you need to do something like the following:
//bundle can be nil if in main bundle, which is default
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyCustomViewController *customVC = (MyCustomViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"customVC"];
//standard way
[self.navigationController pushViewController:customVC animated:YES];
//custom animation
[UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
[self.navigationController pushViewController:customVC animated:NO];
} completion:nil];
You identify the view controller with the identifier you add in the storyboard editor. Below are some screenshots to help show what I mean.

Call storyboard scene programmatically (without needing segue)?

I have a modal storyboard scene that I want to be accessible to all my other scenes. Creating a modal segue to it from every scene on my storyboard creates a big mess of strings going everywhere. Is there a way that I leave off the segues and call the scene programmatically instead?
Basically I want to do something like this:
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
[self presentModalViewController:myNewVC animated:YES];
except instead of creating and pushing a view controller class, I want to do a modal transition to an "isolated" (not connected with a segue) storyboard scene.
Yes you can. Do something like this to get access to the VC, then just Modal Push it:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
Note: the method presentModalViewController:animated is deprecated in iOS 6.
The new code should read:
NSString * storyboardName = #"MainStoryboard_iPhone";
NSString * viewControllerID = #"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller 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.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
I have a case where I want to present a view controller from the main part of the app, one with settings & help & so on. To do this, I want it to be within a nav controller, sort of a little plug in module we can call from a UIBarButtonItem.
Now, this can be to/in the current storyboard, or to another, it doesn't matter.
I want to do it this way, because I loathe the potential of segue line spaghetti all over my storyboard.
Here's how to do it.
- (IBAction)displaySettings:(id)sender
{
LOG_SELECTOR() // google that for extra goodness
// FYI, this can be done using a different storyboard like so.
/*
NSString * storyboardName = #"MainStoryboard_iPhone"; // possibly use device idiom?
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
*/
// To push a new set of scenes with a new Navigation Controller, it is done like this:
UINavigationController *settingsNC = [self.storyboard instantiateViewControllerWithIdentifier:#"Settings Nav Controller"];
OBSettingsUIViewController *settingsVC = [self.storyboard instantiateViewControllerWithIdentifier:#"Settings root"];
[settingsNC pushViewController:settingsVC animated:NO];
[settingsNC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
// Present the view controller;
[self presentViewController:settingsNC animated:YES completion:NULL];
}
In the presented view controllers (or in a subclass of the Navigation Controller), you can have a UIBarButtonItem to then dismiss the whole presented hierarchy of view controllers like so:
- (IBAction)dismissThisVC:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope this helps a bunch of people out. Cheers.
Just call viewcontroller using navigation controller
Write this code in viewcontroller and set viewcontroller in storyboard as set in the image.
ProfileVC *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"ProfileVC"];
[self.navigationController pushViewController:vc animated:YES];
Call to navigate to other class
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *navController = (UINavigationController *)window.rootViewController;
DumpFeed *dump = [storyboard instantiateViewControllerWithIdentifier:#"DumpFeed"];
dump.isPushed=YES;
dump.strUserId = appDelegate.strFriendid;
[navController pushViewController:dump animated:YES];
Heres a Swift version of this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewControllerWithIdentifier("myStoryId")
self.presentViewController(myVC, animated: true, completion: nil)
You should also change your storyboard id like this:
I think that with iOS7 it has become very easy implementing via the storyboard
I'm currently learning about the new features in iOS7 and found this simple solution, but it might have been relevant even in prior versions, I'm not sure.
First u need to connect the presenting VC with the target VC (thats the only connection needed), then within the storyboard's attributes inspector choose the style to be modal, in the identity inspector give your VC a storyboardID and make sure you checked the 'use storyboardID' checkbox,
If its not there yet add this method to your presentingVC:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
YourTargetVC * targetVC =
(YourTargetVC *)segue.destinationViewController;
if(nil != targetVC) {
//Do preparations here
}
}
Now, when you wish to show your targetVC from your presentingVC you can use:
[self performSegueWithIdentifier:(NSString *) sender:(id)];
where the identifier is your viewController's storyboardID, and the sender is the view who triggered the action, this method will invoke the storyboards scene, so the [prepareForSegue: sender:] method will be called allowing u making last modifications before the targetViewController will appear.