Swift 3: make button present ViewController, how? - ios

I would like my button to reshow the user my on boarding screen since it includes some important instructions to my app.
The first time I show my onboarding screen is after the first launch;
My current function looks like this:
#IBAction func anleitung(sender: RoundButtons) {
let appDelegate = AppDelegate()
appDelegate.window?.rootViewController?.present(OnboardingScreen(), animated: true, completion: nil)
}
This is actually like the 4th way I tried this, but, apparently, none of them worked. Do you know how to do that?
Thanks for having a look! Have a great day!

#IBAction func anleitung(sender: RoundButtons) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController?.present(OnboardingScreen(), animated: true, completion: nil)
}

Related

Swift dismissing view with clunky animation

I simply have a ViewController that I would like to dismiss. And this is my dismissAction:
#objc private func dismissView(){
self.dismiss(animated: true, completion: nil)
UserDefaultsService.shared.updateDataSourceArrayWithWishlist(wishlist: self.wishList)
let dataSourceArray = UserDefaultsService.shared.getDataSourceArray()
// update datasource array in MainVC
self.dismissWishlistDelegate?.dismissWishlistVC(dataArray: dataSourceArray, dropDownArray: self.dropOptions, shouldDeleteWithAnimation: false, wishlistToDelete: self.wishList)
}
Proble:
The dismiss animation is very clunky and not fluent at all. I found out that if I remove everything in the function but only call self.dismiss it is working perfectly fine. What is the issue here? Any idea on how I can fix this?
You can try to light-weight load in main thread by
DispatchQueue.global().async {
UserDefaultsService.shared.updateDataSourceArrayWithWishlist(wishlist: self.wishList)
}
And instead of let dataSourceArray = UserDefaultsService.shared.getDataSourceArray() use self.wishList directly in the last line

Swift iOS - how to properly use popToViewController

it's again me - learning swift.
Question is simple, i have control views like this:
[Initial]->[NotLogged]->[SignUp]
Now after sign up i have double dismiss, it's really ugly!
I wan't to go straight from SignUp to Initial page.
I tried this code but sadly it closing app without any error.
self.dismiss(animated: true, completion: {
let controllers = self.navigationController?.viewControllers
for vc in controllers! {
if vc is InitialViewController {
_ = self.navigationController?.popToViewController(vc as! InitialViewController, animated: true)
}
}
} )
pushViewController works same as above, there is output:
2020-01-29 20:30:38.342180+0100 BillyBill[44355:19540995] Can't end
BackgroundTask: no background task exists with identifier 10 (0xa), or
it may have already been ended. Break in
UIApplicationEndBackgroundTaskError() to debug.
Simply just pop to root view controller
self.navigationController?.popToRootViewController(animated: true)
You can just use self.navigationController?.popToRootViewController() command in place of
for vc in controllers! {
if vc is InitialViewController {
_ = self.navigationController?.popToViewController(vc as! InitialViewController, animated: true)
}
}

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

Welcome Screen on Launch

I want a way for the user to have a welcome screen / tutorial on the first launch of the app. If it isn't the first launch of the app, then it open as it usually would.
I already have the welcome screen tied to a button function if the app opens normally. I'm using BWWalkThroughViewController. Here's my code for the button function:
#IBAction func showWalkThroughButtonPressed() {
// Get view controllers and build the walkthrough
let stb = UIStoryboard(name: "MainStoryboard", bundle: nil)
let walkthrough = stb.instantiateViewControllerWithIdentifier("walk0") as! BWWalkthroughViewController
let page_one = stb.instantiateViewControllerWithIdentifier("walk1") as UIViewController
let page_two = stb.instantiateViewControllerWithIdentifier("walk2") as UIViewController
let page_three = stb.instantiateViewControllerWithIdentifier("walk3") as UIViewController
let page_four = stb.instantiateViewControllerWithIdentifier("walk4") as UIViewController
let page_five = stb.instantiateViewControllerWithIdentifier("walk5") as UIViewController
// Attach the pages to the master
walkthrough.delegate = self
walkthrough.addViewController(page_one)
walkthrough.addViewController(page_two)
walkthrough.addViewController(page_three)
walkthrough.addViewController(page_four)
walkthrough.addViewController(page_five)
self.presentViewController(walkthrough, animated: true, completion: nil)
}
func walkthroughCloseButtonPressed() {
self.dismissViewControllerAnimated(true, completion: nil)
}
That code is located in the MyTableViewController.swift file.
Here's what I can't figure out:
I want the view controllers to show on first launch. Once the user finishes the tutorial, they can press the Close button and it will close. I have the code to check if it's the app's first launch. It's located in the AppDelegate.swift file. Here's that code:
// First Launch Check
let notFirstLaunch = NSUserDefaults.standardUserDefaults().boolForKey("FirstLaunch")
if notFirstLaunch {
print("First launch, setting NSUserDefault")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "FirstLaunch")
}
else {
print("Not first launch.")
}
return true
So how do I get the welcome screen to launch on first launch? Do I have to create a function in AppDelegate to handle that, and if so what do I have to do to make the tutorial the initial view controller for just the first launch?
I believe what you need to do is already covered here: Programmatically set the initial view controller using Storyboards. If that doesn't work for you add more notes on why the implementation failed. A google search on "programatically change uiviewcontroller on launch ios" will yield other similar links.

navigation ios 8 swift?

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!!")
}
}

Resources