navigation ios 8 swift? - ios

I am new to the IOS world. I having trouble changing view when im switching to a new view which only can be shown when I login. I am using inside the loginfuction:
#IBAction func loginVerification(sender: UIButton!) {
//Check with the cloud
//temporary faking credentials
var user = "n"
var pass = "n"
if usernameLogin.text == user &&
passwordLogin.text == pass
{
println("Correct credentials")
let homeviewcontroller = HomeViewController()
self.presentViewController(homeviewcontroller, animated: true, completion: nil)
}
else
{
println("Wrong credentials!!")
}
}
The function above is triggered when I press the login button which checks for credentials.
Using the lines above makes the view black. Any suggestions on how to make it work? Any tutorial I can follow on navigation between views? And please don't be so hard on me :)
Thanks in advance!

Hi I would try the Apple tutorials - they are fairly well written and have code samples. Here is one that I used when I was learning: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/SecondTutorial.html. This one is specifically about navigating using Storyboards and Segues. If you look on the left of the page, you'll see links to other tutorials that may be helpful for you when getting started.

Try the following code:
let controller: homeviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("homeviewcontroller") as! homeviewcontroller
self.presentViewController(controller, animated: true, completion: nil)
#IBAction func loginVerification(sender: UIButton!) {
//Check with the cloud
//temporary faking credentials
var user = "n"
var pass = "n"
if usernameLogin.text == user &&
passwordLogin.text == pass
{
println("Correct credentials")
let controller: homeviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("homeviewcontroller") as! homeviewcontroller
self.presentViewController(controller, animated: true, completion: nil)
}
else
{
println("Wrong credentials!!")
}
}

Related

How to solve this error "Warning:Attempt to present whose view is not in the window hirearchy"?

I'm developing an iOS application but I don't have storyboard and I do pure swift code, when I try to check the authentication in the MainViewController and I use perform to go to another ViewController and if token doesn't exist show button in MainViewController I ran into this warning and it won't work.
when I use perform like the snippet below to go to another ViewController by clicking on a button it works just fine.
I've seen all the answer by the title I'm asking here but all the examples has storyboards so it's not related to my question here.
here's a snippet that I'm trying to do in my app.
if defaults.string(forKey: Constants().userTokenKey) != nil
&& defaults.string(forKey: Constants().userTokenKey) != "" {
print("YOU ARE IN ELSE!")
let vc = SelectLocationOnMapViewController()
UIApplication.topViewController()?.present(vc, animated: true, completion: nil)
} else {
UIView.animate(withDuration: 1, animations: {
self.loginRegisterParentView.alpha = 1.0
})
setButtonActions()
}
The problem is here
UIApplication.topViewController()?.present(vc, animated: true, completion: nil)
it seems you use the rootVC which isn't yet fully presented to present another Vc , you need to move this code from viewDidLoad to say viewWillAppear/viewDidAppear

SWIFT 4 Present viewcontroller

I am trying to building a app using the master details template.
in the Master view controller I added a button called Catalogue : this button showing a tabbar controller called Catalogue.
I don't use Segue to show the catalogue, I use the code below to show the tab controller
From Master form I called the Tabbar controller :
#IBAction func Btn_Catalogue(_ sender: Any) {
let AddCatalogueVC = self.storyboard?.instantiateViewController(withIdentifier: "CatalogueVC") as! CatalogueVC
present(AddCatalogueVC, animated: true, completion: nil)
}
From CategorieVC I use the code below to show
#IBAction func Btn_AddCategorie(_ sender: Any) {
self.Mode = "New"
let AddCategorieViewController = self.storyboard?.instantiateViewController(withIdentifier: "AddCategorieVC") as! AddCategorieVC
present(AddCategorieViewController, animated: true, completion: nil)
}
I dismiss the AddCategorieVC using the code below
#IBAction func Btn_Save(_ sender: Any)
{
if self.Txt_CategorieName.text != ""{
self.Mysave = true
self.MyCategorieName = self.Txt_CategorieName.text!
self.dismiss(animated: true, completion: nil)
}
}
I have unwind SEGUE from Save button to a function in categorieVC
#IBAction func FctSaveCategories(_ sender: UIStoryboardSegue) {
let sendervc = sender.source as! AddCategorieVC
if self.Mode == "New" && sendervc.Mysave == true { // Mode insert
let MyCategories = TblCategorie(context: Mycontext)
MyCategories.categorie_Name = sendervc.MyCategorieName
do {
try Mycontext.save()
} catch {
debugPrint ("there is an error \(error.localizedDescription)")
}
}
}
The problem is when I hit the save button in categorieVC the catalogueVC is also dismissing at the same time returning me to the master control.
I am almost sure that the problem came from the Unwind segue but I don't know why.
Update: I activate the Cancel button in AddCategorieVC with the code below
self.dismiss(animated: true, completion: nil)
and when I clicked on it only the AddCategorieVC is being dismissed and I go back to CatalogueVC. The difference between the save button and the Cancel Button is only the UNWIND segue on the Save Button.
And when I add UnWIND segue to the cancel Button (just to test the behavior) it took me back to the master form instead CatalogueVC.
How can I solve that?
And yesss I found it
It look like that unwind segue automaticly handled dismiss contrĂ´le
So all I need to do is remove the dismiss code from the save button this way the unwind segue will took me back to catalogueVC.
.

Change order in Navigation Controller

I'm working with Swift3. I have an App with the VCs as in the picture.
In the Mainmenu-VC the user triggers the Input-segue. User enters a firstname in the Input-VC. This triggers the Select-segue to Select-VC to select a surname and trigger Selected-segue to Details-VC.
From the Mainmenu-VC the user can also access the Details-VC. Back via NavigationControllerMechanism to Mainmenu-VC.
I want to change the NavigationControllerMechanism 'history', so that when the user enters from the Details-VC via the Selected-segue, the previous VC is changed from Select-VC to Mainmenu-VC.
So basically when in the Details-VC, the Back always returns to Mainmenu-VC.
I have tried combining various solutions from the web, without succes.
Is this possible?
Yes it is.
The View-Controller stack is stored in currentViewController.navigationController?.viewControllers.
So you should make something like :
//In Your Details VC :
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
guard let stack = self.navigationController?.viewControllers else { return }
//get the mainMenu VC
let mainVC = stack.first!
// Rearrange your stack
self.navigationController?.viewControllers = [mainVC, self]
//Now you can press "bac" to Main VC
}
you want to change navigation stack in this way you can manipulate
let myprofile = storyboard.instantiateViewController(withIdentifier: "Profile1ViewController") as! Profile1ViewController
let sourseStack = self.navigationController!.viewControllers[0];
var controllerStack = self.navigationController?.viewControllers
let index = controllerStack!.index(of: sourseStack);
controllerStack![index!] = myprofile
self.navigationController!.setViewControllers(controllerStack!, animated: false);
to go to RootViewController
dispatch_async(dispatch_get_main_queue(), {
self.navigationController?.popToRootViewControllerAnimated(true)
})

Swift after signup move to 2nd or any other tab of UITabBarController

In my App after signup/login I want to move control to specific tab of UITabBarController on some conditions. My login or Signup Pages are not in UITabBarController.
Here is the code of my login button I have tried but it only works with identifier or UITabBarController. In my case I want to move to specific tab like 2nd or 3rd tab.
#IBAction func btn_login(sender: AnyObject) {
activityIndicator.progressBarDisplayer("Logging In", true, uiView: self.view)
let timezone = GLib.timezone();
let parameters = ["email": txtemail.text!,"password": txtpassword.text!,"time_zone": timezone]
GLib.fetch("www.abc.com/test/auth/login", parameters: parameters, localkey: "user"){
(isResponse) -> Void in
if(isResponse == true)
{
self.activityIndicator.strLabel.text = "Personalizing Data";
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("some identifier") as UIViewController;
self.presentViewController(viewController, animated: false, completion: nil);
}else
{
self.show_alert("Attention Please!", message: self.GLib.error)
}
}
I have seen many posts but found nothing similar to my mechanism.
I am new to iOS programming so don't know how to figure out this issue.
Using instantiateViewControllerWithIdentifier is the right way to go. However, you need to add the actual identifier, and the UIViewController Custom class type needs to be the actual class.
I'm going to give you a rough example of what it looks like, but it should work. Either way you have to edit your instantiate method as I explain below.
Steps:
Firstly, set the "Storyboard ID" for the viewController in your storyboard. Like this:
Then add this code modified instantiateViewControllerWithIdentifier method:
#IBAction func btn_login(sender: AnyObject) {
activityIndicator.progressBarDisplayer("Logging In", true, uiView: self.view)
let timezone = GLib.timezone();
let parameters = ["email": txtemail.text!,"password": txtpassword.text!,"time_zone": timezone]
GLib.fetch("www.abc.com/test/auth/login", parameters: parameters, localkey: "user"){
(isResponse) -> Void in
if(isResponse == true)
{
self.activityIndicator.strLabel.text = "Personalizing Data";
let yourNewViewController: ViewControllerClassName = self.storyboard!.instantiateViewControllerWithIdentifier("yourStoryBoardID") as! ViewControllerYourePushingTo
self.presentViewController(yourNewViewController, animated: true, completion: nil)
}else
{
self.show_alert("Attention Please!", message: self.GLib.error)
}
}
As a type, you need to actually use the viewController's name.
I asked a similar question a few weeks ago: Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController

How to move from one view controller to another using code on a specific condition in Swift?

In my view controller, upon a specific if condition, I want to move automatically to the next view controller. I tried this code. But it won't work. Please help.
if rightCounter == 5 {
var Level2 = self.storyboard?.instantiateViewControllerWithIdentifier("Level2") as? ViewController
self.presentViewController(Level2!, animated: true, completion: nil)
}
}
if condition == true {
//use either VC Name associate or the Storyboard restoration id
//replace this with yours
if let Level2 = self.storyboard!.instantiateViewControllerWithIdentifier("MainVC") as? UIViewController
{
self.presentViewController(Level2, animated: true, completion: nil)
}
}
I think the problem is with you trying to cast the another VC to ViewController which i presume is the one that you are trying to segue from.
Make sure to have the storyboard ID and you can either type case to UIViewController to be broad or your specific VC to be specific.
Hope it helps.
If you want automatic execution of the conditional statement posted, you could put the code in a property observer for rightCounter.
So where rightCounter is declared change its declaration to include a property observer.
Example could be something like-
var rightCounter: Int = 0 {
didSet {
if rightCounter == 5 {
var Level2 = self.storyboard?.instantiateViewControllerWithIdentifier("Level2") as? ViewController
self.presentViewController(Level2!, animated: true, completion: nil)
}
}
}
With the above code didSet will be called every time rightCounter is set, even if set to the same value as to what it currently is.

Resources