How to move to multiple view controllers? - uiviewcontroller

I want to programmatically move from a viewController to another using Apple's new Swift language. I've googled and read through the docs, and I see how to use a single ViewController. Does anyone have an example or documentation on how to switch between View Controllers?

There are many ways to do this detailed in the class documentation for UIViewController.
Example 1
Here is an example of using the -presentViewConroller: method (assuming this code is written in a UIViewController subclass):
var secondViewController = UIViewController() //create your second view controller.
self.presentViewController(secondViewController, true, NULL)
Example 2
This is how you would present the second UIViewController via storyboards (again assuming this code is written in a UIViewController subclass):
self.preformSegueWithIdentifier("segueIdentfier", self);

Related

presentControllerWithName in tvOS

in watchOS I used presentControllerWithName to show a View Controller and to pass the context in this way
presentControllerWithName("NameOfTheViewController", context:"PassedContext")
Which is the equivalent in tvOS?
Best Regards
As noted in other answers, the way to programmatically show another view controller in tvOS (or iOS) is performSegueWithIdentifier:sender:. (Or presentViewController:animated:completion: if you're not getting your VCs from a storyboard flow.)
But you might not need to do it programmatically. In watchOS it's sometimes easiest to do it that way, but in iOS & tvOS, it's common to make controls directly perform storyboard transitions entirely from Interface Builder. Just control-drag (right-click-drag) from the button to another view controller. (More step-by-step instructions in Xcode Help.)
Unlike watchOS, the view controller transitions in iOS & tvOS don't include a way to pass context information. Not as part of the API, at least — you have to include a bit of glue code yourself to do that. How to do that is a pretty common question.
If you're using storyboard segues (generally, you should), the prepareForSegue:sender: method is typically where you do this — you get a reference to the new view controller that's about to be shown, and use some function or property you've defined on that view controller to pass it some context. It often looks something like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == mySegueIdentifier {
guard let destination = segue.destinationViewController as? MyViewControllerClass
else { abort("unexpected storyboard segue") }
destination.someProperty = someValue
}
}
You can find good examples of this when you create a new Xcode project with the Master-Detail App template.
tvOS is more similar to iOS than it is to watchOS, although they all have some similarities. In tvOS (like in iOS) you can use both performSegueWithIdentifier:sender: or presentViewController:animated:completion: depending on your situation.
For more on this, you can check out the UIViewController class reference.

IBDesignable UIViewController

I'd like to be able to layout my view controller in code but see the layout displayed in interface builder.
I know I can create a UIView subclass, make that IBDesignable, and assign it to the view controller's view, but this would require that I make all other subviews properties of this UIView subclass instead of properties of the view controller.
The real desire is to be able to layout my view controllers in code but quickly see any changes without rebuilding the project. If this is possible with playgrounds instead, an answer describing how to do that would also be appreciated.
Thanks for any suggestions.
I found a workaround to test view controller layout using IBDesignable.
1.Layout your view controller in code just as you'd do normally
2.Create an IBDesignable UIView subclass and add the view controller's view as a subview.
3.Create a xib and set the class of its view to the subclass you created in step 2
To elaborate on step 2, your IBDesignable's initWithFrame: method may look like
{
self = [super initWithFrame:frame];
MyViewController *viewController = [MyViewController alloc] init];
viewController.view.frame = self.bounds;
[self addSubview:viewController.view];
return self;
}
So beyond this initWithFrame method, all of your layout code can be handled by your view controller. You may want to pass data to your view controller in your subview's prepareForInterfaceBuilder method.
In order to layout you own classes in Xcode you need first to import then in your swift playground: here more information.
After you do that, it's came the "tricky" part. In order to make your how class debuggable and visibile in playground, your class must conform to the protocol: CustomPlaygroundQuickLooacable:
Here there is a quick example from the WWDC:
By implementing this protocol, you're basically telling playground how to represent you hown class. I haven't fond any better solution yet.

Table Views. View controller vs Table View controller?

I have been watching a lot of tutorials on Table Views, table view cells etc. When creating a table view why choose a UIViewController over a UITableViewController in Xcode? For instance what would be the main benefits of creating a UIViewController and then adding a tableView object, over just creating a UITableViewController? I have already created a large numbers of scenes within storyboard and I hope I haven't limited myself in doing so. I hope my question is clear as I am new to coding!
UIViewController gives you more control over tableview rather than UITableViewController. You should use UITableViewController only when you are just concerned with tableview in a controller. But if you want to add more subviews/controls in a controller other than tableview, then you have to use UIViewController.
EXAMPLE:
If you just want to display grocery items list with some header and footer, then tableviewcontroller should be priority. But if you want to display mail items in tableview, you would need some additional buttons for altering items in mail(tableview). For later case, you will use viewcontroller. Hope my point is clear.
I think the benefits of having a View Controller is just more versatility and you will have a easier time to add other views other than the UItableview. Whereas UITableViewController i just inheriting a UIViewController, but with the delegates already set up for you. Basically it is assuming you will have a UITableView for sure. In the end it shouldn't really limit you, just cause you used UITableViewController, since everything you want to do in UIViewController, you can do in UIViewController.

Pass information to another viewcontroller without pushing or using segues

I'm trying to "bookmark" (sort of) an object from one UIViewController so that it then shows on a different UIViewController. Currently, the only way I can get this to work is if I either push to the UIViewController that will display the bookmarks or if I perform a segue which is basically the same thing.
Is there a different approach to this?
if you just want to send a variable (without loading the view). This is how I do it.
DetailViewController *detailvc = [[[DetailViewController alloc] init] autorelease];
detailvc.event = [self.fetchedResultsController objectAtIndexPath:indexPath];
And another type of segue I've been doing (without the need of the whole function) i just ask the storyboard to go...
[self.storyboard instantiateViewControllerWithIdentifier:#"detailViewController"];
Just don't forget to name the identifier in the sotyboard, properties, identifier.
Hope it helps...
I've been having some trouble re-learning everything with the storyboard as well.

Referencing Flashbuilder Views by their string name

I have a TabbedViewNavigator application with a navigatory bar which opens view just fine. In each view there is a ButtonBar that will open a new view related to the parent view. I have a single handler which decodes the name of the button and can build a string with the name of the view to be opened. I am looking for some way of referencing the view from this string, in a similar fashion to this["someName"] or getDefinitionByName("someName"). In my code, 'this' refers to the current view and the views that I need to find are not child elements. I don't know where getDefinitionByName() is looking, but it can't find the view either.
I have solved it temporarily with a switch statement, but this is not a good solution. Is there a view collection; if so, who is the owner of the collection or am I not going about this in the correct way.
Thanks for reading this far.
If the view is in a package/folder, you need to supply the full package to getDefinitionByName:
var viewClass : Class = getDefinitionByName("com.us.project.AwesomeView");
var view : DisplayObject = new viewClass();