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 5 years ago.
Improve this question
I have a UICollectionView with 4 Cells, I have a controller for those 4 pages called SwipingController.
I'd like to have a UIViewController for each cell, how can I achieve that ?
I counter advise on what you are going for.
Better just to have Views that manage their layout you don't need controllers to manage layout.
I don't think you will be able to hold a familiar architecture (MVC/MVVM/MVP/VIPER .. ) in the future if you go in this direction.
Also check this out. It will help you understand better the role of a view controller.
Nonetheless you can achieve this with addChildViewController(documentation here)
Add child view
let controller = storyboard?.instantiateViewController(withIdentifier: "test") as! UIViewController
self.addChildViewController(controller)
controller.view.frame = CGRect(0, 44, 320, 320)
self.view.addSubview(controller.view)
controller.didMove(toParentViewController: self)
Remove child view
let vc = self.childViewControllers.last
vc.removeFromSuperview()
vc.willMove(toParentViewController: nil);
vc.removeFromParentViewController()
Related
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 3 years ago.
Improve this question
I am trying to refresh the label in a UITableViewCell without having to refresh the entire table. This is for making a timer-app, where I will be refreshing the labels in the UITableView every second (when it's counting down).
Link to a simulator screenshot of the table view
In the past, I've tried reloading the data of the entire UITableView five times every second, but this results in sliding to delete being very inconsistent and hard to perform. I haven't succeeded in updating individual rows yet, so I don't know if that's a possible solution.
I couldn't find any similar question, but if you know that it's been asked before, please redirect me.
Any help would be greatly appreciated!
Thanks!
The most efficient way in Swift is a callback closure. It causes much less overhead than protocol/delegate or notification.
Run the timers in the data model.
In the model declare a callback property
var updateLabel : ((String) -> Void)?
and call it when the timer fires
#objc func timerDidFire(_ sender: Timer) {
updateLabel?("New Value") // replace "New Value" with the actual value
}
In cellForRowAt assign a closure to the updateLabel property to update the label
let model = datasourceArray[indexPath.row]
model.updateLabel = { value in
cell.textLabel.text = value
}
In tableView:didEndDisplaying:forRowAt: remove the closure
let model = datasourceArray[indexPath.row]
model.updateLabel = nil
If you can find out index of the respective label then you can easily get the cell and access the label and do what ever you want to do
let indexPath = IndexPath(row: 2, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCell {
cell.YourLabel.backgroundColor = UIColor.green // write as per your requirement
}
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 4 years ago.
Improve this question
Pop To viewController : Go to Previos controller
self.navigationController?.popToViewController(AddSalesController, animated: true)
Make sure your controller is in the navigation stack and you can try this code.
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: SOListScreen .self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
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 6 years ago.
Improve this question
I already typed codes for some tools(text/button/label), but once i put a gif background they disappeared. the question is .. How to get them into the foreground?
override func viewDidLoad() {
let filePath = NSBundle.mainBundle().pathForResource("tiger", ofType: "gif")
let gif = NSData(contentsOfFile: filePath!)
let webViewBG = UIWebView(frame: self.view.frame)
webViewBG.loadData(gif!, MIMEType: "image/gif", textEncodingName: String(), baseURL: NSURL())
webViewBG.userInteractionEnabled = false;
self.view.addSubview(webViewBG)
}
Looking at your code I'd say that when you call addSubview method, the view you are adding is on the foreground.
You have to call one of these methods to manipulate view hierarchy :
view.sendSubviewToBack(view: UIView)
view.bringSubviewToFront(view: UIView)
What I'd do is to add the subview, then send it to the background with sendSubviewToBack method
UPDATE :
Seems that it doesn't work that way. There will be several solutions you could use. Either you keep your web view and your view separated but with the same frame so that your view is just on top of your web view.
Problem is that if your frame changes, you have to change the frames of both views.
Another solution is to add your label inside your web view (didn't test it though but it works with an image view so I assume it may work).
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)
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.