This appears to be a storyboard editor issue because it's easy to do in code.
I have a UITableView with two different prototype cells. They have reuse identifiers, different accessories. I want selecting one type to trigger a segue to another view controller, and selecting the other to go to a different view controller. But as soon as I create the second segue in the storyboard editor, the first one is replaced. Even if I have named it.
It seems this should be a common enough scenario, I should be able to have multiple segues, and differentiate them in prepareForSegue.
I need to note, this is easy to do in the didSelectRowAtIndexPath for the view controller's table view delegate, but there I have to explicitly load the template from the storyboard (unless I use a generic and don't need a nib).
Has anyone been able to get this to work in the storyboard editor?
The only way I could think of to do this with how things currently work is by using two different types of prototype cells. (two separate classes) and assigning each different segue to each different subclassed prototype cell.
Related
I just started iOS development last week and I am creating a Table View based application. I having trouble understanding how to use storyboard.
I want each Table Cell to open a different ViewController.
Currently it is setup like this:
Then in the Component View Controller I use if/else statements to determine what content to load. The problem occurs when one of the views needed a TabBar.
How do I assign different View Controllers to each individual cell, rather than one "template" view and forced to add everything dynamically.
First off, I will say that it probably isn't the best idea to assign an individual view per cell. However, if it is what you wish to do, then so be it.
What you would do is create a segue from the table view to the new view by clicking on the table view icon and dragging a segue like so:
2.You would give that segue an identifier like "embedTweetsSegue" or something.
You can then check for the cell being touched and perform the segue programmatically using:
performSegueWithIdentifier("embedTweetsSegue", sender: self)
Ok, I haven't been able to find a definitive answer to this. I'm on Xcode 6 working with Swift and a UISplitView. I have multiple detail views in storyboard and I want to be able to replace my detail view with another when selecting from a tableview. In Xcode 5, I was able to drag multiple segues from my prototype cell and use performSegueWithIdentifier. In Xcode 6, I can only have one segue coming from my prototype cell.
What's the best way to go about this?
One type of cell, one segue. This seems to be a logical and useful system constraint.
One solution is to just create more cell types with different cell identifiers.
Alternatively, if you have another way to determine which segue to use, draw all segues from the view controller instead of the cell. You can attach the necessary row object information in the sender object when calling the segue from the cell selection method.
You can create as many segues as u want from viewcontroller and give each seuge an different segue Identifier in IB, and the call each with performSegueWithIdentifer method
I have a UISegmentedView control with two options. This is part of a MasterViewController. Inside the MasterViewController I have two embedded view controllers, childViewController1 and childViewController2. I have a UIContainerView which is tied to childViewController1. Now I want that when I select the option 2 of the segmented control I should somehow configure the UIContainerView to use the childViewController2.
I'm by no means an expert iOS/Obj-C developer, but I am a bit confused as to what you're trying to accomplish.
What do you mean by change the view controller? You can pass to different view controllers in viewDidLoad. These are wired up in your storyboard, and I think you have to call the navigation controller as well, but I'm not sure.
Either way, I don't understand what you mean by 'segmented' control. Do you have two view controllers? Are you passing data between the two? If so, you'll need to learn about delegates. If not, maybe look at the segue method. My 2 cents.
I am building an app using storyboard. I have added a table view to my view controller, chose static cells, and custom, insertea a title and some pictures. I have also linked each row to a view controller, with modal segue. But I want to be able to add, delete, and reorder the rows. So I added the codes. The only thing is that I can not connect the table view to the codes. If I connect the data source, and run the project I see just an empty table view. So, is there a way to create the table view using interface builder, and just a few codes? otherwise if there are to much codes everything gets messed up.
Connect delegate and datasource to ViewController icon in your Storyboard. Then add UITableviewdelegate and UITableviewdatasource in view controller code. When you connect and do this, then tableview will search its delegate and datasource methods in your code and you will get the output which you expect.
If you want to add, delete and reorder the rows, you can't use static layouts, because those operations make the table view not static!
You will have to implement it using datasource methods and dynamic prototypes. It won't be too difficult. You can still build each type of cell in the storyboard, you can have multiple prototypes with different reuse identifiers.
Background
I'm using interface builder to create the UI for an app I'm working on. The app has a single screen that displays a series of buttons. Clicking on a button displays an associated view which overlays the buttons. Clicking another button hides the previous overlay view and displays another one.
Too make managing the UI easier in IB I've decided to create multiple nib files for each sub view that is to appear when clicking the relevant button. I'm then loading the sub view's nib file in the view controller's viewDidLoad method using the UINib class.
The idea behind this was to avoid having multiple views stacked on top of each other in a single nib file as this would be hard to manipulate in IB. I could have created all the views in code but this would require a lot of tedious coding as the layouts of each sub view are quite complex (with multiple child views).
Example code loading a sub view from a nib file.
- (void)viewDidLoad
{
UINib *aSubViewNib = [UINib nibWithNibName:#"aSubView" bundle:nil];
NSArray *bundleObjects = [aSubViewNib instantiateWithOwner:self options:nil];
// get root view from bundle array
UIView *aSubView = [bundleObjects objectAtIndex:0];
[self.view addSubview:aSubView];
...
The code above is repeated for the other views.
To summarise I have a single screen iPhone app that has layered views that are shown/hidden by clicking buttons. This is achieved with a single view controller with an associated nib file and a series of additional nib files for the sub views which are loaded in the view controller's viewDidLoad method.
Questions!
Sorry for the long introduction but I wanted to be very clear what it is I am doing.
Is my approach bad or unusual?
Are there any potential issues to doing it this way?
What have other people done when they need a dynamic interface and
still want to keep everything in Interface Builder?
Notes
Before anyone asks why don't I just display the sub views on a new screen and use the navigation bar, let me say that I have very good reasons and I do understand iOS UI guidelines. The above use case is not exactly my use case but it's one that clearly describes the problem without getting bogged down in my development app.
Also I know I could have written all the sub views as code but each sub view has a complex layout of child views and it would be a lot of code and messing around to try and get them looking right.
Thanks in advance.
There isn't necessarily a 1-to-1 relationship between view controllers and views. Most views contain many subviews, which are views themselves, so this literally doesn't make sense.
However, depending on the complexity of the views (including their content), you may want separate view controllers... or not.
For example, if you have two sbuviews that are each tableViews, you may want to have one view controller for each tableView. This is because each tableView is looking at the same delegate methods, and if they are in the same viewController, then the delegate methods have to differentiate between the tableViews. The delegate methods have signatures that allow this, but, in my experience, it can really make for a messy code design that is hard to follow and hard to manage.
On the other hand, you may have two tables that are managed by the same viewController, where one table is filled with meaningful data and the other is simply a place holder (as when the data source is empty). One might be visible while the other is not. Why make you life complicated by creating two view controllers when both are driven by the same data source (the model)?
In my mind, it comes down to how difficult it is to follow and manage the code. If the complexity of using a single view controller becomes burdensome, consider using more view controllers.
UPDATE
By the way, I have an example that I am currently working with that may illustrate a similar situation. In the InAppSettingsKit, that a lot of developers use, there are several xib files for pieces of the main view. You can look at the structure here on github. There is one main view controllers and several xib files. (There is also what I would call a "helper" view controller and an email composer view controller.) In this example, the xib files may be used multiple times to specify the layout of table view cells. There is no view controller for each xib file, though. (The documentation for InAppSettingsKit is sparse, so these things may not be obvious just by taking a quick look at it.)
Every View should have a corresponding UIViewController. Using one ViewController to "Control" more than one view breaks the MVC paradigm. "Controlling" multiple "views" from one controller will make it much harder to change one thing without breaking something else. The choices you make on how to present the content to the end user will be different for every individual. So if you say a NavigationController won't work in your case, maybe a Modal view is the answer or, you might just instantiate your custom UIViewControllers and add them to your view ([addSubview:]), if thats the road you want, but like I said, it would be beneficial for you to make a "controller" for each view object along with the corresponding xib. If you need information sent back, use a delegate or use Notifications to send the message back to the parent view. I learned the hard way that not following MVC paradigm, will make you life miserable. Try and keep your code as decoupled as possible. And read up on the MVC design pattern, you won't regret it.
actually its possible to do this.
Open your .xib file,select File’s Owner(in placeholder) -> "identity inspector" (utilities) -> change class name to your controller classname -> press control and drag file's owner placeholder to View object, select "view" in dialog.
Now you can customize your view.
p.s. you can use the same outlets as first xib, you need only to drag them to the new xib(+control sure).
here is an explained tutorial:
http://irawd.wordpress.com/2013/09/05/how-to-link-a-xib-file-to-a-class-and-use-2-xib-files-for-iphone4-and-iphone5/