Using segue from a subview - iphone

I have view which contains a collection view. I have added this collection view programatically (in viewDidLoad) - so that is not on the storyboard. This collection view contains several cells. When a user clicks on one of the cells in the collection view, I want to segue to a different view controller (that I plan to add on the storyboard). My question is - since the collection view is not on storyboard, how can I segue out of that? Or is there other way to accomplish that.
Some updates to the original question above:
In Parent View Controller, I do the following:
//Layout for the collection view
CDLayout *cdLayout = [[CDLayout alloc] initWithItemSize:CGSizeMake(cardWidth, cardHeight) withItemInsets:UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset) withInterItemSpacingX:8.0f withTopMargin:margin withLeftMargin:margin withBottomMargin:margin withRightMargin:margin shouldRotate:NO];
//This is the collection view controller
CDLineLayoutViewController *cdLineVC = [[CDLineLayoutViewController alloc] initWithCollectionViewLayout:cdLayout withItemCount:12 ];
// add the collection view controller to self - the parent
[self addChildViewController:cdLineVC];
[cdLineVC didMoveToParentViewController:self];
// add collectionView as a subview
[self.view addSubview:cdLineVC.collectionView];
The collectionView has 12 cards. When a user clicks on one of the cards I want to move to another view controller.
As you can see the collection view is not on the storyboard. So, is there a way to create a segue?
BTW, one option is what Taseen suggested below. I tried that and it is working. But as I understand it is not actually a "segue."

Can you please show us any code you have written
What i have understood from your question is that you have added collection view in viewDidLoad. and i believe you have set the delegate of collection view to self, So in
didSelectItemAtIndexPath method you can write this code
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//From indexpath.item, you know which cell was clicked
//using switch method
switch (indexPath.item) {
case 0:
//You Can push the view controller from here
//You need to import the header file of that View controller
DestinationViewController *destinationView;
//if the self controller in which the collection view is shown is embedded in Navigation controller,
//you can push using
[self.navigationController pushViewController:destinationView animated:YES];
//if it is not embedded, use modal segue
[self.modalViewController presentModalViewController:destinationView animated:YES];
break;
default:
break;
}
}
EDIT: The segue you will create on the storyboard from your ParentViewController to DestinationController will have segueIdentifier property. like shown below
then in didSelectItemAtIndexPath instead of pushing the controller you can use this code
[self performSegueWithIdentifier:#"collectionViewSegue" sender:self];
you can also configure the destination view controller using prepareForSegue method..
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DestinationViewController *targetVC = (DestinationViewCOntroller *)segue.destinationViewController;
//if you pass anything you can do it here
//if to set any public variable for example image for the imageview
targetVC.cardImageView.image = [UIImage imageNamed:#"queen.png"];
}
This method will be in your parent controller.

Create a new segue by ctrl-click on the initial viewController and drag to the destination viewController. Don't forget to set the segue identifier.
than overwrite the -(void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender method.

Related

Pass value to tableview with navigation controller storyboard

how do i pass a value to a table view controller that has a navigation controller?
i have code to push the new view controller and make it active but no way of passing a value to the table view.
UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
the problem is that i dragged a navigation controller onto the storyboard and it came attached to a tableview. i linked that tableview to my custom tableview class. When i instantiate the view controller from the name "nav", which is the storyboard id of the navigation controller, i get the navigation controller as the view controller being instantiated (newTopViewController); so how do pass a value from where i instantiate the view to the tableview controller?
I know this is already answered (nicely done ttarules), but I thought I'd pass along some extra comments. It's very common to have some type of view controller embedded in a navigation controller. Wrapping a standard view controller in a navigation controller then doing a modal segue to it, gives you a modal scene, with a nice nav bar to put buttons in, etc. Also on the iPad, replace segues can commonly use navigation controllers. It's all about how you design things, but if you know how the controllers stack up and how to easily reference them, you will have more design options. Here is a snippet you can use in your prepare for segue method to easily detect it.
if ([segue.destinationViewController isKindOfClass:[UINavigationController class]]) {
//note: have to get reference to next vc through nav controller
self.nextViewController = [[(UINavigationController*)segue.destinationViewController viewControllers]lastObject];
}
else {
self.nextViewController = segue.destinationViewController;
}
You should pass it inside prepareForSegue if your already going to be segueing to that VC.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"whateverYourSegueIsCalled"])
{
YourTableViewController *tableView=(YourTableViewController*)segue.destinationViewController;
tableView.anyPublicProperty = yourValue;
}
}
EDIT:
Okay so since you actually just have the tableView embedded, you simply need to access the viewControllers property of the navigationViewController..
So it will be something like this inside your navigationViewController class:
YourTableViewController *tableView = (YourTableViewController*)[self.navigationController.viewControllers objectAtIndex:0];
tableView.yourPublicProperty = whateverValueYouWant;
-Your tableView will be the object at index 0 unless you have other VC's also embedded..So you can just print out your vc's and then figure it out from there if you do.

performSegueWithIdentifier causing Navigation to load incorrectly

I have two Views part of a NavigationController hierarchy, one that displays a list of items and the second allows you to edit an item or add a new one.
On the parent view I have an add button on the right of the navigationbar once pressed performs a segue with identity "AddChild". Also, when the user taps on a table item, I perform another segue with identity "EditChild", all set up in storyboard.
In the case the user arrives at the parent view (with the list of items) but no items exist yet, I want to programatically perform the "AddChild" segue, so I added the following code in viewWillAppear: of the parent viewcontroller.
// if there are no children, send user to add screen
if (self.childListData.count == 0) {
[self performSegueWithIdentifier:#"AddChild" sender:self];
}
The child viewcontroller loads fine, but the navigation items are still of the previos view (a back button and an add button instead of a back button and a save button).
This is my prepareForSegue: method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get a reference to our detail view
ChildDetailTableViewController *childDetail = (ChildDetailTableViewController *)[segue destinationViewController];
// Pass the managed object context to the destination view controller
childDetail.managedObjectContext = managedObjectContext;
// If we are editing a child we need to pass some stuff, so check the segue title first
if ([[segue identifier] isEqualToString:#"EditChild"])
{
// Get the row we selected to view
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
// Pass the child object from the table that we want to view
childDetail.currentChild = [childListData objectAtIndex:selectedIndex];
}
}
I'm not sure what I'm missing so if anyone can help, I'd appreciate it.

iPhone: viewWillAppear only when coming back from other views

viewWillAppear is called both when going to the view and when coming back to the view from other views.
I want to select(highlight) and fade-out a cell only when coming back from other views.
Is there a delegate method to do this?
I'm using UINavigationViewController.
If you're on iOS 5, you can use these new properties:
These four methods can be used in a view controller's appearance
callbacks to determine if it is being presented, dismissed, or added
or removed as a child view controller. For example, a view controller
can check if it is disappearing because it was dismissed or popped
by asking itself in its viewWillDisappear: method by checking the
expression ([self isDismissing] || [self
isMovingFromParentViewController]).
- (BOOL)isBeingPresented __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isBeingDismissed __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isMovingToParentViewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (BOOL)isMovingFromParentViewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
In your code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!(self.isMovingToParentViewController || self.isBeingPresented))
{
// animate
}
}
EDIT:
If you're using a UITableViewController, setting the property -clearsSelectionOnViewWillAppear to YES will do this for you. You only have to do it manually if you're using a regular UIViewController with a UITableView subview.
If you are targeting iOS 5, you can use [self isBeingPresented] and [self isBeingDismissed] to determine if the view controller is being added or removed from the nav controller.
I'm also suspecting that you could improve the logic of when you select/deselect the cell in your table view such that it doesn't matter whether the view controller is coming or going.
The usual way to do it is this: when someone selects a row in the table view in view controller A, it gets selected/highlighted and you push a new view controller B. When view controller B is dismissed, you animate the deselection of the table view row in viewDidAppear (so the user can see it fading out) in view controller A. You wouldn't worry about whether view controller A has just appeared or is re-appearing, because there would only be a selected table view cell in the appropriate case.
viewWillAppear is getting called when the view appears
after the viewDidLoad
after you dismiss or pull a view controller
You could change the viewWillAppear to the following
- (void) viewWillAppear:(BOOL)animated
{
static BOOL firstTime = YES;
if (!firstTime)
{
//Do your alpha animation
}
firstTime = NO;
}
In your UINav Controller you could create a "lastView" property and have each of your view controllers (that are controlled by your UINav Controller) set this property on "viewWillAppear"... in your target view... the one you want to do the highlighting and fading you could check this property of the UINav Controller and see if it's NIL or not.
That's just one way to do it. This wouldn't work if you pop up a modal or the like.

How to pass data(selected/clicked cell value) from TableViewController's Cell(Disclosure Detail) to the next ViewController loaded?

I have a TableViewController with multiple cells(detail disclosure) each associated with unique data(eg. title/details). When I click one of the cells another ViewController gets loaded(via modal or push segue) and should display the data depending on which cell was clicked.
Simple Eg:
Each Cell of TableViewController are associaed with separate urls. When I click one of the cells(detail disclosure), the next ViewController that contains a WebView should open the web page corresponding to the url of the cell clicked.
How can we achieve that?
One way of accessing the data is to use the following inside the prepareForSegue method:
NSIndexPath *indexPath = tableView.indexPathForSelectedRow;
This will give you the access to the selected row (cell) of the table so that you know which item your user has selected.
Then extract any data you need and pass it to the destination view controller using the prepareForSegue method as shown in the other answer.
First, you need to set the identifier of each segue (in the storyboard, just click on the segue and set it in the Attributes Inspector).
Then, in your UITableViewController subclass, you need to override prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"yourSegueIdentifier"] ) {
UIViewController *viewController = segue.destinationViewController;
viewController.yourVariable = yourData;
}
}

Programmatically copy array to new view in Storyboard

I am currently trying to copy an array I have to a new view that I am creating programmatically. I actually have found how to do this with normal navigation controller syntax. My issue is I'm using the new storyboard and I don't know the syntax to do the same thing. Here is the code I have..
CustomerListViewController *second = [[CustomerListViewController alloc] initWithNibName:#"CustomerListViewController" bundle: nil];
[second setValue:customerList.list];
// [self.navigationController pushViewController:second animated:YES];
[self performSegueWithIdentifier:#"LoginSegue" sender:self];
as you can see, I am programmatically creating the second view controller and storing the local array customerList.List to the created view controller's array variable. The next step is to open the new created view. The line commented out is the syntax to open the view under a navigation controller. The line below is the storyboard way, but minus specifying the view I created. I need to know the syntax for the storyboard to do the same thing as the navigation controller.
It does not make sense to create your own instance of CustomerListViewController here if you're using segues. The segue itself will create the view controller from the storyboard and the instance you have created here will do nothing.
Instead, just call performSegueWithIdentifier:sender: here. Then implement the prepareForSegue:sender: method like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"LoginSegue"]) {
CustomerListViewController *destinationController = (CustomerListViewController *)segue.destinationViewController;
[destinationController setValue:customerList.list];
}
}