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.
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 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.)
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 6 years ago.
Improve this question
I created a navigation controller and put 2 view controllers linked to it. One is called FirstLaunchVC and the other is FirstLaunchVC2, I want the user to put his name in the text field nameTxtField and when he clicks on the continueBtn it should lead to the other. In this second view (FirstLaunchVC2) there is a label called nameGreetings which will show the name of the user as a greeting with prepareForSegue, the thing is that it's crashing, saying that there are breakpoints on the line of the performSegueWithIdentifier and on the line where I write "nextVC.nameGreetings.text = "(str) etc etc". I have no clue why, can anyone help me with that? Btw, I've already checked the identifier and it's correct.
Is nameGreetings an IBOutlet? You cannot set the IBOutlet controls for the destination view controller in prepareForSegue of the originating view controller because while the destination view controller has been instantiated, its views and IBOutlet references have not. The prepareForSegue should feel free to update String properties of the destination, but not its IBOutlet references.
So updating of name is fine, but the nameGreetings should not be set in prepareForSegue, but rather that should be deferred until the the viewDidLoad of that FirstLaunchVC2.
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.
I has many UITableViewController subclasses in my app.
Now i just needed to modify them all to add +1 row in all cases, and one simple equal row in all.
I do not want to modify all of them by hand, better way seem's to replace UITableViewDataSource method to modify values in way like:
+(void)load {
[[self class] jr_swizzleMethod:#selector(tableView:numberOfRowsInSection:) withMethod:#selector(swizzledTableView:numberOfRowsInSection:) error:nil];
}
- (NSInteger)swizzledTableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self swizzledTableView:tableView numberOfRowsInSection:section] + 1;
}
But it replaces superclass function, that does not called in subclasses, so this is not working. Is there method to do that what i want, without modifying all subclasses?
You'd need to swizzle every subclass specifically. You can find them by introspecting the class hierarchy at runtime with objc_getClassList, but I can't begin to describe how dangerous and fragile this approach is. You're trying to apply this to every tableview in the system, which you hope is just the tableviews you mean it to be (i.e. your tableviews). But what about tableviews that might be used by the system or from third-party libraries? You're modifying them, too. And when you try to understand the crash this causes, the stack trace will be unintelligible because of the swizzle.
In order for this to work, tableView:cellForRowAtIndexPath: also needs to correctly handle this extra row, so it's hard to see how every table view controller in the system is going to be implemented correctly without knowing about this +1.
Either subclass your table view controller (and have them call super), or use a separate object that all of them call to add the extra row if it's needed. This other object (or superclass) is also where you should handle the cell for this extra row.
I have little experience in swizzling. But I have two possible solutions to your problem.
First:
Create a subclass: YouBaseTableView: UITableView, and add a row in YouBaseTableView. And inherit all your table view classes from YouBaseTableView.
Second:
Create an extension for UITableView, and write your row in this extension.
I'm probably late for the train...
But for future reference, a solution for the problem would be to swizzle setDataSource of UITableView and replace it with an NSProxy instance.
Usually nobody overrides the setDelegate / setDataSource methods, and that would allow you to swizzle those and intercept all calls to these delegates and exchange the implementation.
Check this out for more info: https://developer.apple.com/documentation/foundation/nsproxy
You are going into two different areas that need full understanding to be used correctly and are highly dangerous: Performing code in the +load method, and using method swizzling. I would never dare doing anything in +load. +initialize is ok if you know what you are doing, but +load is something you mustn't even think of touching if you ask questions here.
Now ask yourself first: What is "self" in a class method, and what is "[self class]"? Do you think this has even a chance of working?
I'd also recommend that you google for "swizzle" and pick up some other code for method swizzling. It looks quite dubious to me. And writing it as a category instead of a plain C function feels just horrible.
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;