Why can't you rename the body property? [closed] - ios

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 last month.
Improve this question
In swiftUI, in order to conform to the View protocol, the conforming struct has to implement the body property (specifically, its getter, since body property is read-only).
Can I change the name of body property to something else, say presentation?
struct ContentView: View {
var presentation: some View {
Button("Hello SwiftUI!") {
}
}
}
This doesn't work. I get 'Type 'ContentView' does not conform to protocol 'View'.
In UIKit, when conforming to the UIApplicationDelegate protocol, I was able to change the name of the UIWindow variable. The documentation has the name 'window', but when conforming, I changed it to mainWindow and things worked.
Why can't I change the name of body property of a swiftUI view? Am I missing something?

Can I change the name of body property to something else
No. A protocol's requirements are required. To conform, you must obey them to the letter.
when conforming to the UIApplicationDelegate protocol, I was able to change the name of the UIWindow variable
That variable isn't a requirement. Note the marking optional on the page you yourself linked to.

Related

why can NSObject class be delegate to UIImagePickerController for instance? [closed]

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
i need explanation of how could NSObject Class be a delegate to any controller although NSObject is not a controller !?
A delegate is just an implementation of Delegate design pattern. In Cocoa classes it is implemented using Protocols - any class that implements a specific protocol can be a delegate. For example, let's look at the definition of delegate property in UITableView class (in Objective-C, because it's better at showing distinction between classes and protocols):
#property(nonatomic, weak) id<UITableViewDelegate> delegate;
In Objective-C, id is a universal pointer - a pointer to object of any class. You can see that UITableView expects it's delegate to be of any class, but a class that implements UITableViewDelegate protocol
The only requirement for a class to be a delegate for a particular class is to implement the protocol required. It doesn't depend upon its inheritance chain. It could be uiviewcontroller, uitableviewcontroller or simply the NSObject.
If your class confirms to the required protocol then its a fair candidate to be the delegate.
An object can be a delegate without being a controller.
Being a delegate just means that you agree to respond to a set of methods. It doesnt matter what kind of object you are, as long as you understand the methods in the delegate protocol, you can be a delegate.
Say, for example, you have a central table view manager object that knows about mulitple table views throughout your UI. You could make that table view manager the delegate of all the table views in your app. (I don’t know if this would make sense, but you could certainly do it if you had a reason to do so.)

How to handle UITextField value change without delegate? [closed]

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
There is a method in UITextFieldDelegate
textField(_:shouldChangeCharactersInRange:replacementString:)
Which allows me to do it. But I want custom text field handle this automatically to update its state without delegate and I don't want to use delegate internally as it can be overridden. How should I do this?
For example there is a method - becomeFirstResponder which I override to show/hide validation error when text field resigns from being first responder, but there seems to be nothing for handling text property changes, or was I inattentive?
You could use UITextFieldTextDidChangeNotification.
Or you could subclass so you can be your own delegate and also forward delegate calls to another delegate which is added (so your internal delegate is a proxy for the real delegate, handling the call and then forwarding it).

How to assign a set of methods to UITextFieldDelegate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to know how to fire a method when a particular text box is edited.
For example, it is explained here how to assign a certain format to a textbox, using a number of functions. However, I don't know where to copy-paste these functions. In the explanation it says to use UITextFieldDelegate, but I don't know where it is.
I have a AppDelegate class, a ViewController class (including its xib), and a UITextField in the xib. I want the code to work for this UITextField.
Add those methods in your ViewController.m class
and then in your viewDidLoad method of ViewController.m Class
[yourTextField addTarget:yourTextFieldDelegate
action:#selector(reformatAsCardNumber:)
forControlEvents:UIControlEventEditingChanged];
set reformatCardNumber: to be called whenever the text field fires a UIControlEventEditingChanged event
also add delegete in Viewcontroller.h file
and Thats Done.

Changing UIView To be instance from UIControl Programmatically [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a button that will change the view's class to UIControl
i have typed this line of code but Xcode say it is a error
-(IBAction)button:(id)sender
{
self.view.class = [UIControl class];
}
So guys my question is how to change my view's class to UIControl class programmatically
In objective C you cannot assign the class property of an object and within instance of UIViewController (which I'm assuming you're code resides in) you cannot assign the view property outside of loadView without causing issues.
I think your general question on changing an objective C's class is slightly misguided and you may need reword your question such that it's possible to suggest a way to do what you're trying to do in cocoa.
What are you actually trying to do? Are you attempting to change what's displayed on screen in response to an event? Are you trying to change the behaviour of your view controller's view somehow?
These things are usually done by adding/changing/modifying the view hierarchy of your view controller by adding other UIView instances rather than modifying existing ones.
You can define your own class that inherits form UIControl easily but there's a lot more you need to do to begin using it in the above example.
#interface CustomClass : UIControl
…
#emd
Can I suggest the following introduction to Objective C and iPhone programming guides form Apple that may shed some light on how to do things in Cocoa.
Learning Objective C - A Primer
Programming with Objective C
iOS App Programming Guide
You cant change the class type at runtime.
Alternatively you can set your control to stop handling user interactions:
self.userInteractionEnabled = NO;

What is a delegate in iOS? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been flashing through some questions here about xcode iOS programming, and I've seen more and more people say something like "as the delegate of blabla" things like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
currentLocationAnnotation = [annotation retain];
}
But I could never help me because I don't understand what this means, and where I have to put it, if I put it in my .m file, it doesn't work, and if I put it in my AppDelegate.m it doesn't work either.
Please help :)
In your code this is the custom delegate what it means it just work like a helper object in which whatever methods had implemented inside custom delegates can be used.
It's a design pattern where you register a object(the delegate), that confirms to a specified protocol, with another instance of some other class that then calls the first objects "protocol methods"(delegate methods) when it wants the delegate to perform some work. This is usually used to avoid unnecessary subclassing when a object just wants to "outsource" part of it's work.
It's a bit hard to get in the beginning. A delegate of a class is like the handler of events of that class. For example, for the map view class, you set a delegate (e.g. your custom class), and in your custom class, start implementing the handlers (it doesn't necessarily need to handle events. For example table view asks its delegate about how many rows it will have, their height etc.). For example, in the example code you've posted, the map view is asking its delegate to return the view for the annotation object that it's sending as a parameter. You need to implement your logic in your class to return the appropriate object.
In English terms, think of it as an email: 'Hello, my delegate, as a part of your job, I'd like you to give me the view for annotation that I'm attaching below, Thanks, Map View.'
Of course, it requires an immediate response from the delegate right now, and the 'attached below' is the 'annotation' parameter. Hope it helps.

Resources