I have 2 views A and B. View A contains a table of 1 section with 2 rows. Each row has each own picture....My task is how to display in view B a picture of row 1 when you click on row one. If you click on row 2, view B will display picture of row 2. Please show me the way to pass data from one to another view using seque.
Say you have controllers ViewControllerA and ViewControllerB for the two views and also assuming that you have created a segue between the views in storyboard with an identifier
ViewControllerA should implement this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (segue.identifier.length > 0)
{
if ([segue.identifier isEqualToString:#"your_identifier_name"])
{
ViewControllerB *viewControllerB = (ViewControllerB *)segue.destinationViewController;
//And you can pass data between the two controller.
viewControllerB.currentRow = self.selectedRow;
}
}
}
jajo87's answer will work, but if you're asking, you don't have a full grasp on MVC design. Both ViewControllers should be pulling the images from a single model (a custom subclass of NSObject in a file you create with command-N). That model should have a property that represents the image you picked. Both ViewControllers can have pointers to the model; pass them along when you instantiate ViewController B.
Related
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.
I am new to iOS Development and Objective-C in general. Currently I am working on an app that contains 3 tabs. As you can see from the screenshot below the first tab contains a Table View with 2 cells.
So, after the user taps on one of the cells another detail view appears with buttons which later will be changed with pictures of the products that are going to be listed there.
My question is: How do I connect the buttons with another view that will contain a table view with some details?
What you need is a Grid View to display your products. Use GMGridView. Its brilliant...
In your storyboard, hold down the control key and drag from your cell to the view controller you want to connect it to. Then set the segue style to "push." Set the name of your new segue. Then in code set the product data in the -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// GET THE SELECTED ROW
NSInteger selectedRow = [[self.tableView indexPathForCell: sender] row];
// PASS THE CORRESPONDING DATA TO THE NEXT VIEW CONTROLLER
if (segue.identifier isEqualToString: #"MySegue") {
MyViewController* myvc = (MyViewController*)segue.destinationViewController;
MyProductData* selectedData = [_productData objectAtIndex: selectedRow];
myvc.productData = selectedData;
}
}
So once the user taps a product image button, you want to display more detail about that item?
In your image grid view controller, just create a method:
- (IBAction)imageButtonAction:(id)sender {
// do something like check the [sender tag]
// to determine which button sent the message
// then instantiate the detail view controller and push
}
then connect this method to the Touch Up Inside action in Interface Builder. Rather than a proliferation of action methods for each button, you could give each button a tag and route all of their Touch Up Inside actions to the same method.
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.
I have a very simple application that has only 2 view controllers. The initial root controller has two UIButtons on it, and they both transition to the 2nd view controller via Storyboard. The information that is loaded on the 2nd view controller is determined by a buttonPressed IBAction that is linked to both the initial view controller. The problem I am having is that once I tap one of the two buttons, I get the 2nd view controller loading up blank. From debugging, I notice that the buttonPressed function is called after the 2nd view is loaded. How can I make it so that the buttonPressed function is called BEFORE the 2nd view controller is loaded blank?
Hardly any code involved, I have both UIButtons tagged 1 and 2, and the button press function loads two text files depending on the sender tag equaling 1 or 2. Everything is linked up in Storyboard. Any suggestions?
*buttonPressed function:
if([sender tag] == 1)
count = 1;
else
count = 2;
The count determines the file to be loaded.
*In Storyboard, I have just 2 view controllers, (No navigation or any of that) and two UIButtons on the initial view and a text view on the second to display text. The UIButtons are connected to buttonPressed and to the 2nd view controller segue.
Don't bother with a IBAction for button pressed. Have the buttons segue to the new controller and use prepareForSegue:sender: to figure out which button was tapped (the sender).
You can then get a reference to your controller that's about to appear with something like...
SecondController *next = (SecondController *)[segue destinationViewController];
...and tell it which file to load, which it can do in viewDidLoad:.
This sounds like a case for prepareForSegue!
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"First Button Segue Identifier"])
{
ViewController *theViewController = segue.destinationViewController;
theViewController.textProperty = self.firstButtonText;
}
else if ([[segue identifier] isEqualToString:#"Second Button Segue Identifier"])
{
ViewController *theViewController = segue.destinationViewController;
theViewController.textProperty = self.secondButtonText;
}
}
There you go, it is pretty generic because I didn't know your variable names or what text you are using. Similarly, if the segues don't have seperate identifiers, you can use the sender to determine which button is being pressed.
Good luck!
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;
}
}