How to open a view using a button - ios

So I have a tabbed application, and I'm really new to xCode. I created a first view.
So I need help with code for the Quotes button to help lead me to the tab bar controller. Thanks

It looks like you have a programmatic ("custom") segue in the view controller. This probably isn't what you want.
In Interface Builder, select that segue and in the Attributes Inspector, change the kind of segue to "Show (e.g. Push)".

First of all, it looks like you're using a custom segue and it looks like you don't actually need it in your situation.
There is a simple solution by changing the segue Kind to Show (e.g. push). This will simply show the connected view controller on top of your first viewcontroller.
This won't allow you to navigate back to the first view controller. If you want to do so, click on the first view controller in your storyboard and go to Editor > Embed in > Navigation controller.
This will automatically add a navigation controller which will manage the default navigation for you.

click on button press and hold control and drag it to another view controller some actions will pop than select the first option show and you are ready to go.

let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let tb = storyboard.instantiateViewControllerWithIdentifier("Storyboard ID") as! UITabBarController
self.presentViewController(tb, animated: true, completion: nil)
To return from TabBarController
self.dismissViewControllerAnimated(true, completion: nil)

Related

Bar item button not displayed at runtime but visible at design time in a modal view

I have being trying different things and looking around for a while without finding an answer to my problem. Maybe I'm doing something fundamentally wrong.
The sample application consists of:
A first view controller that displays a second view controller using a segue. This works fine.
A second view controller, in which I have simulated the display of a third view programmatically, which contains a bar item button (named "Done") that I would like to display.
The bar item button in the third view controller is not displayed at runtime but is displayed in IntefaceBuidler at design time.
This third view controller needs to be displayed modal.
What I'm doing wrong to display this bar item button?
A sample project illustrating the problem is available here.
Below a screen capture of the bar item button at design time:
Below a screen capture of the bar item button not showing at design time:
PS:
Please disregard the "Unknown class ThirdViewControlller in Interface Builder file.", since the ThirdViewController is displayed fine at runtime. Also, the "Done" button in the middle of the view works fine.
In SecondViewController you need to push the third onto the navigation controller stack like so:
self.navigationController?.pushViewController(thirdViewController, animated: true)
You are currently presenting it as a modal. Also, you've unnecessarily added a second UINavigationController to your storyboard (for the third view controller)
If you want to present a modal, then you'd need to embed the controller in a navigation controller:
let navController = UINavigationController(rootViewController: thirdViewController)
self.present(navController, animated: false)
If you prefer to keep this within the storyboard, then you need to provide a identifier for the UINavigationController and insatiate that in your function.
The above button is a navigation bar item that will only be displayed on the navigation bar . For achieving your desired result , you first have to embed the navigation controller at least in your second viewcontroller and then you should do a push segue rather than modal . Navigation controller can be added by
whith your second viewcontroller selected go to Editor\Embed In\Navigation Controller
for pushing the viewcontroller programatically onto user navigation controller's stack use
self.navigationController?.pushViewController(nextViewController, animated: true)

Swift create 'universal' segue in tab bar app

I'm a swift newbie and I've got a simple question which I hope someone can help me figure out.
I have a multi-tab app. I created some segues from the tab view controllers on the Stopryboard. I've given the segues identifiers and I'm calling them from my Tab1ViewController code using performSegueWithIdentifier("tab1ToMyTarget", sender: sender) no problem.
However, I'd like to be able to call the segue from any of the app's tabs without creating new segues from the other tabs' view controllers (i.e. I don't want to create "tab2ToMyTarget" - I presume there's a better way!).
My question: Do I create these 'universal' segues on the tab bar view controller (e.g. "tabBarToTarget") (and if so how do I call it from one of my tab view controllers)? ...or...
Do I keep the segue from a single tab view controller (tab1ToTarget) and call that segue from a sibling tab view controller somehow?
First, set the view controller that you want to go to's storyboard Id. Then run this:
let vc = storyboard!.instantiateViewControllerWithIdentifier("someViewController")
self.presentViewController(vc, animated: true, completion: nil)
You cannot use the same segue to open a controller from a different one. It's better to instantiate a view controller with its storyboard id and then present/show based on the flow.

How to use a UITabBar with separate storyboards in iOS8/Swift?

So basically I have several storyboards that contain "reusable" content (like a form and some other things). I have another storyboard that has a UITabBarController with one tab that contains a TableViewController and a detail view, a second tab that opens a Settings view, and potentially a third button that opens the form (referenced earlier in my question) modally. My question is...how can I create this third tab within my Tab Bar Controller storyboard since the view that it opens resides in a separate storyboard (as in I can't drag a segue like is the normal process)?
You can instantiate a view controller programmatically from a storyboard by using the instantiateViewControllerWithIdentifier method of the storyboard that defines that view controller.
To get an instance of your form, you may do something along the lines of this:
let storyboard = UIStoryboard(name: "YourFormStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("formViewController") as! YourFormClass
You then populate the tab bar controller with its root views as you always have, laid out here in the Apple docs:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/

Display extra view controller without removing the tab bar

My app has a menu and a UITabBarController. What I want to do is to display a view controller that belongs to my menu but not to the UITabBarController, However I don't want to remove the UITabBarController. I tried codes similar to the one below but they are removing the UITabBarController.
tabBarViewController.selectedViewController?.presentViewController(ExtraViewController, animated: true, completion: nil)
You should get UINavigationController of selected ViewController and then push view you want to present. Otherwise you are presenting modal view controller with presentViewController witch hides your UITabBarController view.
Im not at my computer right now, and cant post any code, but hope this helps.
Best way to do it is to use UINavigationController. You can create new one programmatically and put your menu controller as root.
And if you put this UINavigationController as one of views in UITabBarController then you can preform a code like:
[self.navigationController pushViewController:ExtraViewController animated:NO];
Also you can use storyboard to create your controllers hierarchy like that:
To do that select your menu controller and go to Xcode menu>Editor>Embed in>Navigation Controller and then Xcode menu>Editor>Embed in>Tab Bar Controller.

Xcode IOS transition views with a tab view controller

I am developing an IOS app using Swift and have a tab view controller. I start the app at the SecondViewController to allow the user to insert their information, upon clicking save I would like to programmatically transition to a different view. When I use the following code, the view transitions however the tab bar disappears.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let home = storyBoard.instantiateViewControllerWithIdentifier("home") as FirstViewController
self.presentViewController(home, animated:true, completion:nil)
Should I not be presenting the view and just using a different form like segue or something?
Again this is a tab bar controller.
Thanks
Using presentViewController method, your particular view controller get's displayed modally above your current tab bar and navigation stack.
To keep your tab bar visible you either have to push your new view controller on the current navigation stack by calling the method pushViewController(..) on your current navigation controller, or - this is actually the right thing in your particular situatuin, assuming that you defined you FirstViewController in your storyboard and connected it to the first tab in your UITabBarController— you can simply tell your tabBarController to select the particular segmentedIndex (in this case 0).
Something like this should help:
self.tabBarController.selectedIndex = 0

Resources