2.Page ViewController are not fullscreen? - ios

im on my first app with swift right now. and I want to make 2 Pages.
1.Page -> Button -> 2.Page
it works but the 2.Page looks not like fullscreen ?! more like a Page to slide?
i wann a normal 2.Page like the first one. hope you know what I mean :D thank you for your help.
enter image description here
enter image description here

You just have to go to story board select the 2nd view controller and change presentation style to "Full Screen". Please refer this

If your SecondViewController is created in programmatic code without the storyboard the following code will work without problem
Solution1:
class FirstController: UIViewController {
func presentSecondController() {
// create instance of secondVC
let secondController = UIViewController(nibName: nil, bundle: nil)
// Change presentation style
secondController.modalPresentationStyle = .fullScreen
// Present secondVC
self.present(secondController, animated: true, completion: nil)
}
}
`
Solution2:
2.a If SecondViewController is created in the storyboard, you will have to add a StoryboardID to your SecondViewController like the following image:
2.b instantiates secondController instance from the storyboard by adding this code:
class FirstController: UIViewController {
func presentSecondController() {
// Create storyboard instance from name file
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Instantiate secondVC from storyboardID
guard let secondController = storyboard.instantiateViewController(withIdentifier: "secondControllerID") as? SecondController else { return }
// Change presentation style
secondController.modalPresentationStyle = .fullScreen
// Present secondVC
self.present(secondController, animated: true, completion: nil)
}
}

Related

open new screen on button click in swift

I'm newbie in swift iOS. In my storyboard, I've created some dynamic fields from viewController. Have created a button from object library.
Now I need to open an new empty screen after clicking this button.
In the new screen, there'll be some input fields. I'll create those through programming or object library.
But I'm unable to open the empty screen.
#IBAction func SaveTgw(sender: AnyObject) {
print("test=====")
}
I need to include the screen opening code within SaveTgw. Any help or any descriptive tutorial link would be appreciated....
1-From Utilities open identity inspector in storyboard ID enter id "MySecondSecreen"
2-In your method add the following code :
#IBAction func SaveTgw(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let vc = storyboard.instantiateViewControllerWithIdentifier("MySecondSecreen") as! UIViewController; // MySecondSecreen the storyboard ID
self.presentViewController(vc, animated: true, completion: nil);
}
Create a SecondViewController in Main.storyboard like this -
On Scan button click , do following code
#IBAction func scanAction(sender: AnyObject) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.presentViewController(secondViewController, animated:true, completion:nil)
}
There are many answers available, just do a search -
Swift: Navigate to new ViewController using button
Swift - Change view controller using action button
Do simple way. Connect segue from firstViewController to secondViewController.Next click the segue goto show attribute inspector (top 3rd option from right side).In identifier give "your text".
write below line into your button action.
performSegueWithIdentifier("your text", sender: nil)
just use this , with code
swift 2,3x,4x
if nib exist,
let vc =
YourViewControllerName(nibName:"YourViewControllerName",bundle:nil)
self.navigationController?.pushViewController(vc,animated:true)
if no nib
let vc = YourViewControllerName()
self.navigationController?.pushViewController(vc,animated:true)
SWIFT 4
Based on #AbedalkareemOmreyh's answer:
Go to Utilities, open identity inspector and enter the ID "MySecondSecreen" in the storyboard ID.
Add the following code to your button:
#IBAction func SaveTgw(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let vc = storyboard.instantiateViewController(withIdentifier: "MySecondSecreen")
self.present(vc, animated: true, completion: nil);
}

loading different container view from container view

I have 3(cv1, cv2, cv3) container views place on same position on mainViewContoller.
All 3 having different viewController classes (.swift)
Button is placed on cv2(secondViewController), on that button click I want to change containerView from cv2 to cv3.
For more clear, how can I call mainViewController method from secondViewController (cv2)?
secondViewController.swift
#IBAction func nextButtonClick(sender: UIButton) {
// Want to call action of firstViewController which is changeContainerView()
}
firstViewController.swift
funct changeContainerView(){
loginContainerView.hidden = true
signUpContainerView.hidden = false
}
Like that.
You should use the delegate pattern. It's very common in the Cocoa framework. See the documentation if you don't know how it works.
Try this
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let svc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.presentViewController(svc, animated: true, completion: nil)
})
Yout need just change mainstoryboard name if it needed and name of destination VC in 2 places

How to load UIViewController programmatically from storyboard?

I created a UIViewController in my Main.storyboard with a few buttons and labels. I'm trying to switch to that view controller using self.presentViewController but it will not load the view from storyboard. It will only load a blank black screen by default. Any idea on how to load the view from what i've created in storyboard?
self.presentViewController(ResultViewController(), animated: true, completion: nil)
The way you're doing this just creates a new instance of your view controller. It does not create one from the prototype you've defined in Interface Builder. Instead, you should be using this, where "SomeID" is a storyboard ID that you've assigned to your view controller in Interface Builder.
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SomeID") as? ResultViewController {
presentViewController(resultController, animated: true, completion: nil)
}
You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.
Full Swift 3 code including instantiation of Storyboard:
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
if let mainViewController = storyboard.instantiateInitialViewController() {
present(mainViewController, animated: false, completion: nil)
}
SWIFT 5.2
first, you should define a storyboardID for viewcontroller and after use this code
let storyboard = UIStoryboard(name: "Main", bundle: nil) // type storyboard name instead of Main
if let myViewController = storyboard.instantiateViewController(withIdentifier: "myViewControllerID") as? CourseDetailVC {
present(myViewController, animated: true, completion: nil)
}

How to switch view controllers in swift?

I'm trying to switch from one UIViewController to another using code. Right now I have,
self.presentViewController(ResultViewController(), animated: true, completion: nil)
All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?
With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Without storyboard
let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
You can try this one
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Make sure you set your view controller identifier.
Swift 3
To present a controller modally
let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)
To show a controller
self.show(vc, sender: self)
Try this:
let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)
Hope this helps.. :)
The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:
self.performSegueWithIdentifier("segue", sender: nil)
this will trigger the segue that will go to your resultViewController
The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen.
Make sure you are using a UINavigationViewController.
Source: ios UINavigationController Tutorial.
Shaba's answer is a good start but requires the following so that you
can control the segue from within your view controller:
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
// your code here
// return true to execute the segue
// return false to cause the segue to gracefully fail }
Source
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = vc
Or
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: vc) //If you want to add navigation functionality to that VC

Go to a nested ViewController from a main TabBarController programmatically in Swift

I have the following ViewController structure, see image below.
Whenever I want to move from ViewController 1 to any of the other main controllers I use self.tabBarController?.selectedIndex = indexNumber
// for instance, this would take me to ViewController 3
self.tabBarController?.selectedIndex = 2
Based on the picture below, how can I go from ViewController 1 to TargetViewController programmatically when a button is tapped in ViewController 1?
FYI -
In the picture below I'm showing a button in ViewController 3 this is just to show the storyboard structure, the actual button-tap will happen in ViewController 1
EDIT:
Here is how you do it based on Prashant Tukadiya's answer.
ViewController 1
In ViewController 1 add the following in your tap event.
self.tabBarController?.selectedIndex = 2
let nav = (self.tabBarController?.viewControllers?[2] as? UINavigationController)
let vc = TargetViewController.viewController()
nav?.pushViewController(vc, animated: true)
TargetViewController
In your TargetViewController add the following class method.
class func viewController() -> TargetViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "targetViewControllerID") as! TargetViewController
}
Add targetViewControllerID in the Storyboard ID field.
I was facing same before a couple of days and finally, I got a solution maybe this will help you.
TabController.shared.tabcontroller.selectedIndex = 1
let navigation = TabController.shared.tabcontroller.selectedViewController?.navigationController
let SavedAddress = self.storyboard?.instantiateViewController(withIdentifier: "SavedAddressVC") as! SavedAddressVC
navigation?.pushViewController(SavedAddress, animated: true)
I have used the same solution. and this is the working code for me.
You can directly give identifier to the TargetViewController from storyboard and load from storyboard then and push or present it.
like add this method in your TargetViewController
class func viewController () -> TargetViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "yourIdentifer") as! TargetViewController
}
and on tap event
let vc = TargetViewController.viewController()
self.navigationController?.pushViewController(vc, animated: true)
EDIT
After reading comments got clear idea about your requirements
On button action from ViewController1, You want to goto TargetViewController but when press back you want back to viewController 3
Select particular index first
self.tabBarController?.selectedIndex = 2
After that you need to grab UINavigationController
let nav = (self.tabBarController?.viewControllers?[2] as? UINavigationController)
then Push ViewController
let vc = TargetViewController.viewController()
nav?.pushViewController(vc, animated: true)
Note: Don't forgot add identifier to storyboard to TargetViewController and also add class func viewController () -> TargetViewController method to TargetViewController
Hope it is helpful
In action function of a button in ViewController 1 , you can use this :
func presentMethod(storyBoardName: String, storyBoardID: String) {
let storyBoard: UIStoryboard = UIStoryboard(name: storyBoardName, bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: storyBoardID)
self.definesPresentationContext = true
self.present(newViewController, animated: true, completion: nil)
}
//usage
presentMethod(storyBoardName: "Main", storyBoardID: "TargetViewController")

Resources