I have a UICollectionViewController and UIViewController, with the UINavigationController as root.
When I click on a UICollectionViewCell, it pushes the UIViewController
The snippet of code I have for that is:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UICollectionViewCell *cell = (UICollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
MyViewController *myVC = (MyViewController *)segue.destinationViewController;
}
But it crashes when I click on the cell and says
"Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController"
I thought by making the UINavigationController root solves this. What am i doing incorrectly?
Just figured it out actually
went into Editor at the top toolbar, then Embed In, then navigation controller
voila
Related
I have the following files:
Main.storyboard
This is the same as the default storyboard created when creating a new project with a few additions: There is a button in the view which is connected to the button outlet of the view controller (instance of ViewController) and the doTheThing: action on the view controller.
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (weak) IBOutlet NSButton *button;
#property (weak) NSViewController *controller;
#end
#implementation ViewController
- (IBAction)doTheThing:(id)sender {
if (self.controller) {
NSLog(#"Removing %#", self.controller);
[self.controller.view removeFromSuperview];
[self.controller removeFromParentViewController];
} else {
self.controller = [[NSStoryboard storyboardWithName:#"Another" bundle:[NSBundle mainBundle]] instantiateInitialController];
[self addChildViewController:self.controller];
[self.view addSubview:self.controller.view];
NSLog(#"Adding %#", self.controller);
}
}
#end
Another.storyboard
A simple storyboard containing a single scene (view controller + view) that is set to the initial controller and is an instance of AnotherViewController. There is a label in the view that is not connected to any outlet.
AnotherViewController.m
#import "AnotherViewController.h"
#interface AnotherViewController ()
#property (weak) IBOutlet NSTextField *label;
#end
#implementation AnotherViewController
- (void)dealloc {
NSLog(#"Deallocing AnotherViewController %#", self);
}
#end
When I run the app and click the button, it adds the view controller and view from Another.storyboard, and when I click the button again they are removed and the instance of AnotherViewController is deallocated.
However, if I connect the label in Another.storyboard to the label outlet on the AnotherViewController, the deallocation never occurs. Why is this and what can I do to fix it?
Edit: I do have a few workarounds, but they aren't very desirable and I would prefer to understand why the recommended way (storyboards and outlets) isn't working properly.
Undesirable workaround 1: Do the same thing but load the view controller and view from a XIB file. This works as expected, but ideally I would be able to do the same thing using storyboards.
Undesirable workaround 2: Bind all my outlets manually in code in the view controller's viewDidLoad method. This is just tedious and ugly as it requires iterating through all the view's subviews and comparing identifiers.
Apple have confirmed (via bug report) that this is a known issue and will be fixed in OS X 10.10.3.
I'm pretty new to iOS. I'm building an app and am running into an issue. I have a navigation controller with a table view controller atop its stack. When I select a row in that table view controller, what I'd like to see is a collection view with the following:
The nav bar with the name that appears on the selected cell as the navigation item title.
A collection view as the main interface
a tab bar with the collection view, and an imagePickerController
Here's what my code looks like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NewTabBarController *tbc = [[NewTabBarController alloc] init];
UIImagePickerController *takeAPicture = [[UIImagePickerController alloc] init];
UITabBarItem *tabItem = [takeAPicture tabBarItem];
[tabItem setImage:[UIImage imageWithContentsOfFile:#"CameraIcon.jpg"]];
[tabItem setTitle:#"Take a photo!"];
UICollectionViewFlowLayout *photoFlow = [[UICollectionViewFlowLayout alloc] init];
PhotoCollectionViewController *photoHub = [[PhotoCollectionViewController alloc] initWithCollectionViewLayout:photoFlow];
[tbc setViewControllers:[NSArray arrayWithObjects:photoHub, takeAPicture, nil]];
NSArray *items = [[items accessor] allItems];
Item *item = [items objectAtIndex:[indexPath row]];
[photoHub setItem:item];
[photoHub useLayoutToLayoutNavigationTransitions];
[[self navigationController] pushViewController:tbc animated:YES];
}
Then in my PhotoCollectionViewController implementation I have:
#syntesize item;
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationItem *itemHeader = [self navigationItem];
[itemHeader setTitle:[item itemName]];
UITabBarItem *tabItem = [self tabBarItem];
[tabItem setImage:[UIImage imageWithContentsOfFile:#"itemImage.jpg"]];
[tabItem setTitle:[NSString stringWithFormat:#"Photos of %#", [item itemName]]];
}
My problem is that when I select the cell, The collection view loads, and I can see the cells I have set up in the collection view, but the nav bar item has no title, and the tab bar item has "Photos of (null)" and no image. The "Take a photo!" text appears, but the image does not.
Do you guys have any idea how I can restructure this to make everything flow correctly. I must be doing something wrong in the way I'm utilizing tab and nav controllers.I don't want there to be any tabs until this stage in the app, which is 3 or 4 VCs in already. Should I be using a tab bar controller from the App Delegate onward?
The problem here is that you are pushing tab bar controller onto a navigation controller stack. The view controllers of a tab bar will have a navigation item, but their navigation items aren't shown when the view controller is on screen. Instead, the tab bar controller's navigation item is on screen.
You could use self.tabBarController.navigationItem, but then each view controller will have to modify the navigation item every time it's brought on/off screen, which is really messy.
If you're going to use a UITabBarController, I would recommend either presenting it modally, or having it be the root view controller on your UIWindow. It's tough to get it working right as a view controller in a navigation controller's view controller stack.
Your tab bar item not showing its name is a separate issue. It's because viewDidLoad is getting called before you set your item instance, specifically it's getting called when you call [tbc setViewControllers:[NSArray arrayWithObjects:photoHub, takeAPicture, nil]];
You can confirm this by breakpointing in view did load, where you'll see that item is nil. If you haven't already, you should overload your setItem: method in PhotoCollectionViewController, and have that method also update your UI.
I'm trying to get a reference to my detailViewController in application didFinishLaunchingWithOptions: method. But here I am faced with a problem.
I have next Views structure is storyBoard (for iPad):
UISplitViewController --> UINavigationController --> UITableViewController --> detailViewController
I created segue with "push" style between UITableViewControllerCell and detailViewController
Screenshot: http://picturepush.com/public/13071076
I'm trying to get a reference to detailViewController in this way:
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
DetailViewController *detailViewController = [navigationController.viewControllers lastObject];
But in result I got reference to my UITableViewController, instead of acceptable
DetailViewController. The navigationController.viewControllers count is 1 but there should be 2 (I think so).
I hope for your help.
Since you created a segue with push style, the detailViewController will only be loaded after you tap on a table view cell. It will not be loaded on the app didFinishLaunchingWithOptions method.
I would suggest you try to make the detailViewController as a property in your table view controller.
#property (nonatomic, strong) UIViewController *detailViewController;
Then in your app didFinishLaunchingWithOptions method, you can access the property:
UITableViewController *tableViewController = [navigationController.viewControllers lastObject];
DetailViewController *detailViewController = tableViewController.detailViewController;
But you will need to avoid using perform segue function and push the detail view controller whenever it is needed with the usual pushViewController method.
[self.navigationController pushViewController:self.detailViewController animated:YES];
I am presenting a popover with my viewcontroller:
UIBarButtonItem *barButton = (UIBarButtonItem*)sender;
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
[self presentPopoverFromBarButtonItem:barButton andController:settingsViewController andSize:CGSizeMake(RECT_POPOVER_SETTINGS.size.width, RECT_POPOVER_SETTINGS.size.height)];
and inside this view controller I have a button that pushes another view controller.
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
Now in this view controller I have an action, and when it's done I do:
[self.navigationController popViewControllerAnimated:YES];
Now the strange behavior is that when I pop the view controller, the
viewWillAppear
method gets called in SettingsViewController, then the breakpoints come back to pop method inside my anotherViewController.
This is a universal App so I am using this same setup on the iPhone, but without the initial UIPopOverController. This is functioning properly in the iPhone (with no popovers, pop method gets called first, then viewWillAppear), but on the iPad the viewWillAppear gets called first, then pop.
So my question is, can somebody explain why this is happening?
Thanks.
I create a split view project, on the iPhone's storyboard, i drag a bar button item to the navigation right and change the style Add, and i drag a view controller to the storyboard, so i use control-drag to build the link, and change the uiviewcontroller's custom class "AddViewController", when i run the project and push the DetailViewController, it is black view.
And i have input the code on the AddViewController.m
-(void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"AddViewController"];
[vc setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentModalViewController:vc animated:YES];
}
i found the reason
On the my create "AddCreateViewController", i should comment the function.
- (void)loadView
{
// If you create your views manually, you MUST override this method and use it to create your views.
// If you use Interface Builder to create your views, then you must NOT override this method.
}