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 1 year ago.
Improve this question
I have a ViewController with a progressView, label and button. A task runs in background in task.swift when user clicks on the button on ViewController. I want to update the label and progressView in ViewController based on progress of tasks in task.swift.
Swift Communication Patterns
You have multiple ways of setting up communication between 2 classes in Swift:
Method call / return
Delegate pattern
Closure
Notification pattern
Publishers subscribers
Respect MVC (Model - View - Controller)
Because you want your model (here Task) to not be aware about the ViewController in order to respect MVC, you cannot apply just a simple method call through the reference.
Pick the right one
However all the others ways are totally valid. Because it seems to be a 1 to 1 relationship between ViewController and Task, the most simple solutions would be to use a delegate or a closure. If your classes relationship would have been a one to many, you could have picked notification. Publishers would be for reactive programming and if you like Combine.
Related
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 2 years ago.
Improve this question
I'm new to swift and programming in general. I'm working on an app I didn't write, and am struggling to understand the existing code. To avoid dealing with protocol errors, I want to know if it's possible to reuse the functionality of a UIButtons IBAction on one view and call it's IBAction function in another view. If so How would I go about doing this. Your help is appreciated!
ContextScreen
https://controlc.com/1a91efca
(the button I'm trying to reuse)ContextMenuScreenViewController [a https://controlc.com/90847f01
the original location of the buttonOrderCreateViewController [a https://:controlc.com/3b52eefc
the original location of the refer order button(OrderCreateViewController)
ContextMenuScreenController: where I'd like to reuse the refer order button
Yoy can easily move the logic from IBAction to som function and call this function in IBAction and from any View what you need!
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 7 years ago.
Improve this question
I would like to know the efficient way to create an app in Swift. Will the number of files (ViewController files) in my Xcode project affect the performance of my app?
For example, for a simple arithmetic calculator app, in the first view I'll display 4 buttons for addition, subtraction, multiplication and division. If I click on a button, then it will open a new view to enter the data and process it.
So, now my question is, should I create 4 different viewControllers or should I use only one viewController to perform all the 4 operations?
Which one would be the efficient way?
First, your question isn't about Swift, it is about UIKit.
Second, you are looking at the problem the wrong way. Different view controllers will control different views. Switching from one view to another is a user action. Do you want the user to switch from one view to another, or do you want them to use one view all the time without switching?
If there is one view displaying different things, it's one view controller. If it's several views displaying different things, it's several view controllers. And I can assure you that the number of view controllers is not what makes your app fast or slow.
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
Difference between subclass and category? Which place used effectively in both items?And difference between category and protocol? Need real time example.
Thanks
Really short explanation. I can point you in a direction at least.
Subclass
Inherit functions of a class. You want to have the same behaviour as the class you just subclassed but with some of your own custom functionality.
Category
Add extra functionality to a class that fits your needs. In other words you could add a method to a class (NSString for example) that lets you count the characters. This can later be called on your NSString class.
Protocol
You can delegate tasks to another class that has set itself as a delegate. You can force classes to implement certain methods.
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 9 years ago.
Improve this question
I realize similar questions have been asked before, most of them are for outdated version of ios and do not completely answer the changed aspects from the version.
What types of objects should we set up in these methods in a viewcontroller to get the best performance?
I would like a detailed answer like:
Declaring int variables at x is good because y and so on.
I would like an explanation for NSString, NSInteger, UIImage, UI elements, graphics, network calls, coredata calls.
What kind of objects are we encouraged to get rid of at viewwilldisappear?
If all these are stated together in a complete answer, it would be useful for everybody.
Edit:
Difference between viewDidLoad and viewDidAppear
Bad question
Answer good but does not include initiliaze and viewdiddisappear
init method vs. a viewDidLoad type method
Question and answer left out viewdidappear and disappear
iOS: What is the difference between -init and -viewLoad of a ViewController?
Outdated, uses nibs.
iPhone dev - create array in init or viewDidLoad
I do not agree with the answer, something probably changed from 2009.
Init:
Instantiate any objects that you class will use. Do not add them to the view if they are to be subviews you must do this in viewDidLoad after the view has loaded.
ViewDidLoad:
At this point all you views have been instantiated so you can make any modifications, add subviews etc.
viewDidAppear:
Means what it says. If you want to change a background picture ever 5 seconds, I would start the timer here as you know the view is being seen by the user.
ViewDidDisappear:
The view is not currently being displayed -- so tidy up anything you don't need.
There are lots of other posts that have more detail if you search.
Link to Apple Doc (the first point of call)
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 9 years ago.
Improve this question
Hi I'm new to Xcode and don't quite understand the difference between all the view controllers. Eg what is the difference between a uiviewcontroller with a collection view on it and a uicollectionview controller it self? Confused as to why I wouldn't use a view controller then add views onto it depending on my requirements eh table, collection etc. Can anyone help to shed some light?
It's based on your requirements. You may change some properties with your View controllers. But when you use as part of its' controllers, you can't change it.
For example you CanĀ“t change UICollectionView Size of UICollectionViewController(see this).
In these case, you must use UICollectionView in viewcontroller. As like this, if you want to customize some property but functionality are same in all.
Here I mention some ref for you: Ref
You can take UICollectionViewController as a template which will have every thing for you. And view controller is simple scracth pad, you have to add things on it. There are some pros and cons in view controller you have to do every thing yourself, but collectioViewController you got controller with collectionView. But you can not change things as easily in it.