If you are using PFLogInViewController with the PFLogInFieldsSignUpButton option enabled how do you customise the look of the PFSignUpViewController?
You need to subclass PFSignUpViewController, and perform your layout customisations in viewDidLoad and viewDidLayoutSubviews.
class SpuggySignUpViewController: PFSignUpViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customisation here
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// More customisation here
}
}
To get your custom version of PFSignUpViewController presented by the PFLoginViewController, just instantiate it, and set it in the PFLoginViewController.
let loginController = PFLoginViewController()
let customSignupController = SpuggySignUpViewController()
loginController.signUpController = customSignupController
// Now show your login Controller
Related
How can I change the back button of a certain navigation controller. I have tried to use
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.tintColor = UIColor.red
}
I know that if i use navigationController it will change the back button tint color on all of my view controllers.
Try this!!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = .red
}
override func willMove(toParent parent: UIViewController?) {
self.navigationController?.navigationBar.tintColor = // original color
}
}
EDIT: I have decided to change the way my app works, so this problem is solved. Thanks to everyone who helped!
I have a modal controller where when I press a button it dismisses the view. What I want to do is change a variable in another view controller when I dismiss it, is that possible? Or, if this doesn't work, is there a way for me to access the changed variable of another swift file? I will add my code below:
class PopupViewController: UIViewController {
var event = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func dismiss(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
#IBAction func event910(_ sender: Any) {
event = "storyTime"
dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! ViewController
vc.event = event
}
}
I want to pass the changed variable "event" to another view controller, how can I do this?
Delegate View Controller is as follows. : -
it is the place where you will send the data to the next swift file
protocol myprotocol {
func anyfunction(_ param1:String)
}
struct mystruct1 {
var delegate:myprotocol?
// where you want tot start the delegate / send the data to the next file
func anymethod(){
delegate.anyfunction(sendTheDataYouWant)
}
}
// it is here you will receive the data
class anyclass:UIViewController ,myprotocol {
let class1 = mystruct1()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
class1.delegate = self
}
func anyfunction(param1:String){
// here Save the data you want
// because this function will be triggered as delegate will be called
}
}
ps:- I reccomend you to read https://docs.swift.org/swift-book/LanguageGuide/Protocols.html
& apple docs
I have an app with 6 UIViewControllers.
ANY viewcontroller features a function like this one:
#IBAction func onHelp(_ sender: UIBarButtonItem) {
DispatchQueue.main.async(execute: { () -> Void in
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = "MapHelp"
helpVC.helpSubtitle = "Map"
self.present(helpVC, animated: true, completion: nil)
})
}
Any IBAction in any viewcontroller presents the same HelpViewController but passing different parameters (starter and helpSubtitle).
Since I don't like to repeat code, first of all I thought this function should be converted to something more generic.
But: is there any way to create a generic IBAction, working for every viewcontroller?
Create a BaseViewController and add the generic method there.
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func genericMethod(starter: String, helpSubtitle: String){
let helpVC = self.storyboard?.instantiateViewController(withIdentifier: "Help") as! HelpViewController
helpVC.starter = starter
helpVC.helpSubtitle = helpSubtitle
self.present(helpVC, animated: true, completion: nil)
}
#IBAction func onHelp(_ sender: UIButton?) {
//You can use this method as generic IBaction if you want. It can be connected to buttons of all child View Controllers. But doing so will limit your param sending ability. On the plus side though, you won't have to define an IBAction everywhere and you can simply connect your child VC's button to Parent Class' IBAction.
}
}
Now inherit your ViewControllers from this class like:
import UIKit
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller", helpSubtitle: "I was triggered from VC1")
}
}
and
import UIKit
class SecondViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btnTapped(_ sender: Any) {
genericMethod(starter: "View Controller 2", helpSubtitle: "I was triggered from VC2")
}
}
That's it. Both your ViewControllers can call the parent method. If you still want to use the generic IBAction, you can do that too but I'd not recommend that course given that you want to pass params that can vary. If you wanted to do it though, it would look like this:
Bear in mind, the ViewController here has been inherited from the base ViewController which is why it can access the IBActions defined in the parent class. All you have to do is drag and connect.
I have the following SearchController:
class SearchController: UITableViewController, UISearchResultsUpdating
The implementation works well, but I have to tap on the search bar to start searching every time I load the controller. What I'd like is to activate the search bar once SearchController is loaded. I have tried with:
override func viewDidLoad() {
super.viewDidLoad()
...
...
self.resultSearchController.searchBar.becomeFirstResponder()
}
I also tried with:
override func viewDidAppear(animated: Bool) {
self.resultSearchController.searchBar.becomeFirstResponder()
}
But the search bar remains deactivated and the keyboard won't show up. What am I missing? Thanks!
This works (not sure why it's not working for you):
class ViewController: UIViewController {
let mySearchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = mySearchController.searchBar
mySearchController.hidesNavigationBarDuringPresentation = false
mySearchController.searchBar.becomeFirstResponder()
}
}
I am Try to Create extension for UIViewController but Its not working . i am changing status bar color for using extension but i cant get success. But without extension is working. I am new in Extension Please help me.
My Aim is I DONT WANT TO CREATE FUNCTION ON EVERY VIEW CONTROLLER , REDUSE REUSABILITY
Create Extension Is Not Working
extension UIViewController {
func preferredStatusBarStyle_change() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
}
//!!!!!!!!!view1
override func viewDidLoad() {
super.viewDidLoad()
//preferredStatusBarStyle();
self.preferredStatusBarStyle_change();
}
//!!!!!!!!!view2
override func viewDidLoad() {
super.viewDidLoad()
//preferredStatusBarStyle();
self.preferredStatusBarStyle_change();
}
Without Extension IS Working
View1
override func viewDidLoad() {
super.viewDidLoad()
preferredStatusBarStyle();
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
View2
override func viewDidLoad() {
super.viewDidLoad()
preferredStatusBarStyle();
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
Help me Try to understanding Reusability and extension
It not working because:
Viewcontroller will call preferredStatusBarStyle() for define about statusbar. Actually you remove preferredStatusBarStyle(); in viewDidload, your controller still work.
So in your case you can resolve like this:
You create a subclass of UIViewController maybe name it is: BaseViewController and it this class you put:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
And now all your ViewController you inherited from BaseViewController status bar will always .LightContent you don't need put this code above every where.
Demo: Demo