Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I use a class function I implement to perform a segue if the class it is held in is not connected to a viewController?
In order to perform a segue, you need the instance of view controller where the segue originates from - for example:
class MyViewController {
}
class MyClass {
showNextView(fromViewController: UIViewController) {
fromViewController.performSegueWithIdentifier("segue_id", sender: fromViewController)
}
}
However I discourage this kind of interaction. Don't tell the view controller how to show a view - let it do on its own, and just ask it.
So, I suggest creating an instance method in your view controller:
class MyViewController {
...
func goToNextView() {
performSegueWithIdentifier("segue_id", sender: self)
}
}
and use that to make the transition:
class MyClass {
showNextView(fromViewController: MyViewController) {
fromViewController.goToNextView()
}
}
You can instruct the currently visible view controller to segue using "performSegueWithIdentifier".
You obviously need a reference to the visible controller.
Inside your method use:
currentViewController.performSegueWithIdentifier("push", sender: currentViewController)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I currently have the following function, once clicked, it takes the user to the ‘homeViewController’
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
However, I am looking to use this function when a user logs out to take them to the ‘UIViewController’, however whenever I use UIViewController instead of homeViewController in the above code, it produces the following error.
Type 'Constants.Storyboard' has no member 'UIViewController'
What do I need to do in order to make this work and take the user to the very first viewcontroller for the app?
Here’s the view of the view controller I am trying to redirect the user to.
You need to change the Class value from ViewController to HomeViewController, as well as the Storyboard ID to HomeViewController for the initialisation to work.
Custom Class
Class HomeViewController
Identity
Storyboard ID HomeViewController
Then can instantiate the view controller like this:
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(identifier: "HomeViewController") as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to MVVM. How should I update the data model in parent view model from child view model?
As an example, suppose I have a CollectionViewModel for a Newsfeed page, it keeps an array of Post (my date model) and an array of CollectionCellViewModel that corresponds to individual posts. Each cell makes network request to listen for new likes. How can I pass this information back to CollectionViewModel and update Post?
You achieve that with multiple ways:
Delegation pattern:
protocol CollectionCellViewModelDelegate {
func onAction()
}
class CollectionCellViewModelDelegate: YourType {
var delegate: CollectionCellViewModelDelegate?
/// Your custom action
func yourCustomAction() {
delegate?.onAction
}
}
then assign .delegate in your parent class and implement onAction() method
Closures:
class CollectionCellViewModelDelegate: YourType {
var yourAction: (()->())?
func yourAction(_ completion: (()->())?) {
yourAction = completion
}
/// Your custom action
func yourCustomAction() {
yourAction?()
}
}
then call your closure with following code from parent class:
child.yourAction { // do custom stuff }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my Swift project, ViewController #1 has a route of destinations listed in a table. User is driving along and we keep track of where user is using the built-in CLLocation stuff. But user has now navigated away from ViewController #1 to some other portion of the app and is now in ViewController #2. A certain event that I track in ViewController #1 fires (in my example; the user has reached the next destination on his list). I need the app to immediately segue back to ViewController #1 right when that event fires. How can it be sensed when user is in ViewController #2?
NSNotificationCenter and an Unwind Segue seems to be the way to go, this way this functionality can be reused, all you would need to do is register for a given notification on the relevant view controllers.
Something along the lines of:
class ViewControllerOne: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
//- your event happens
SomethingHappened {
NSNotificationCenter.defaultCenter().postNotification(CallForUnwindSegue, object: nil)
}
}
class ViewControllerTwo: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
override func viewDidLoad() {
super.viewDidLoad()
//- Subscribe for notifications
NSNotificationCenter.defaultCenter().addObserver(self, #selector(self.doSomething), name: CallForUnwindSegue)
}
//- Unsubscribe for notifications
override func deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func doSomething(notification: NSNotification) {
//Handle your event here.
}
}
There's a good unwind segue article here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to handle the pressing of the back button on a UI Navigation bar. I have an extension in objective C (https://github.com/onegray/UIViewController-BackButtonHandler) and i have bridged it to my project in Swift using the header, now I do not quite know how to implement the code in Swift. This is the implementation in C:
-(BOOL) navigationShouldPopOnBackButton {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return NO; // Ignore 'Back' button this time
}
return YES; // Process 'Back' button click and Pop view controler
}
func navigationShouldPopOnBackButton() -> Bool {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return false // Ignore 'Back' button this time
}
return true // Process 'Back' button click and Pop view controller
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there an option to observe and get the information about appearing and disappearing?
I want to grayscale my elements like apple ui-elements by appearing of UIAlertController!
Since now i found out that the "_UIBackdropViewComputeAndApplySettingsNotification" was called and contains userInfo about the appearing view.
You are going to make the UIAlertController's view appear, so how can you not know? You don't need to observe it; you're doing it (by calling presentViewController...).
That takes of what happens when the alert appears. What about when it disappears? Well, it disappears because the user tapped a button. You get to write the handler for every button in the alert. So again, you know when the alert is disappearing, because your handler is running.
The solution is, everything works automatically. You just have to implement..
override func tintColorDidChange() {
self.setNeedsDisplay()
}
..and of course working with the tintColor
Thanks matt for quick answer!
To expand on the other answers: each of your UIView subclasses should implement tintColorDidChange to be notified of the change.
Here's a sample implementation:
class someLabel : UILabel {
override func tintColorDidChange() {
let isInactive = self.tintAdjustmentMode == UIViewTintAdjustmentMode.Dimmed
if (isInactive) {
// modify subviews to look disabled
self.textColor = UIColor.grayColor()
} else {
// modify subviews to look enabled
self.textColor = self.tintColor
}
}
}
A few other good code samples (albeit in Objective-C) can be found in this SO question.