Extension method on UIView that will be called upon init - ios

I'm trying to write an extension method on UIView that will be called whenever a view is initialized, meaning that the method should be invoked without my having to call on it explicitly. Is there a way to accomplish this such as with key-value observing?

Related

Objective C: Access Variable From View Controller To NSObject

I am started learning objective c again, and I am trying to access a variable from my main view controller in my NSObject class.
How would I do that?
For example I have declared
UITextField *name;
and I want to use it in my NSObject.
The best practise would be implement delegate pattern here. Suppose you want to change the value/content of UITextField, then fire the delegate with the value. And in ViewController class, set the delegate and implement that delegate method. In that delegate method, update value/content of UITextField.
One more thing, Please make sure to update UITextField's content/value in main thread. UI related changes should be always done in main thread.
You can refer to the NSObject class via a method with the plus sign '+' +(type)anyMethod; to get the variable. If I correctly understood what you mean

How to call -(void)drawRect:(CGRect)rect

How Can i Call
-(void)drawRect:(CGRect)rect
in https://github.com/stkim1/MTImageMapView/blob/master/MTImageMapView/MTImageMapView.m
From Another UiViewController Periodically With NSTimer
From the documentation:
Discussion
The default implementation of this method does nothing. Subclasses
that use technologies such as Core Graphics and UIKit to draw their
view’s content should override this method and implement their drawing
code there. You do not need to override this method if your view sets
its content in other ways. For example, you do not need to override
this method if your view just displays a background color or if your
view sets its content directly using the underlying layer object.
...
If you subclass UIView directly, your implementation of this method
does not need to call super. However, if you are subclassing a
different view class, you should call super at some point in your
implementation.
This method is called when a view is first displayed or when an event
occurs that invalidates a visible part of the view. You should never
call this method directly yourself. To invalidate part of your view,
and thus cause that portion to be redrawn, call the setNeedsDisplay or
setNeedsDisplayInRect: method instead.
YES i Found The solution i am calling a function with timer at the End of a drawrect function and calling setNeedsDisplay()in it

Can I call a different init method with UICollectionViews dequeueReusableCellWithReuseIdentifier:forIndexPath:?

In the .h file of UICollectionView a comment says:
// If a class is registered, it will be instantiated via alloc/initWithFrame:
Is there a way that I can have it call a custom init method?
How about [cell configureForData:data] right after you dequeue it? You probably need to re-configure the data on dequeued cells, anyway.
Suppose there is a way that you can have it call a custom init method, you wouldn't pass anything to the custom init method by dequeueReusableCellWithReuseIdentifier:forIndexPath:, so setting up the data should be separate from the init method.

Inheritance: Restricting rather than extending? [duplicate]

This question already has answers here:
Is it possible to make the -init method private in Objective-C?
(9 answers)
how to block a superclass method to be called to a subclass
(5 answers)
Closed 9 years ago.
Suppose you have a UIView subclass. You define an init method "myInitWithFrame: ... andWhatNot:...". You know you won't be using the init method inherited from UIView ever and your custom init method does some vital custom initialising so that you want to force client classes to never use the inherited initWithFrame method.
Is it possible to hide the standard initWithFrame method that was inherited from UIView?
Actually, you can get compile-time warnings about calling a method on a subclass. Use the __attribute((deprecated)) attribute. If you want people to use -initWithPizza: instead of -initWithFrame:, do this:
#interface MyView : UIView
- (id)initWithPizza:(MyPizza *)pizza;
#end
#interface MyView (Deprecations)
- (id)initWithFrame:(CGRect)frame __attribute((deprecated("Use initWithPizza: instead")));
#end
Putting the -initWithFrame: declaration in a separate category is necessary to avoid Xcode complaining that you declared the method in the header but didn't implement it. Since you're just inheriting it from the superclass, that's fine; you don't have to implement it at all. But if you want to implement it to throw an exception, or call through to -initWithPizza: with a default argument, that's fine.
Of course, this won't stop UIKit from calling -initWithFrame: if it was already going to do so. But if you can guarantee that won't happen, then you're fine.
Actually, you CAN restrict with a subclass. You can override whichever methods you want to block in your subclass's .h file. You can make initWithFrame unavailable by placing the following in your .h file.
- (id) initWithFrame:(CGRect) frame __attribute__((unavailable("message")));
This will make the initWithFrame: method unavailable to anyone using your subclass.
To keep other code form calling this method, you can further restrict by putting this in your .m file.
- (id) initWithFrame:(CGRect) frame
{
return nil;
}
No. You can't prevent the users of your subclass from calling the methods of a superclass. You could override them and throw an exception inside, but that would just produce a broken subclass.
Remember that inheritance works as an "is a" extension, that is, instances of your subclasses should behave normally in any context that doesn't know about this particular subclass but knows about its superclass. It's only in places that have explicit knowledge about your subclass that you can benefit from adding extra initialization and other methods.
For example, UIKit has no knowledge of your subclass. So if you want to make your UIView subclass available from a NIB, you need to use the initialization methods that will be called by the NIB loading system, namely initWithCoder:. You can simply call your own initialization methods inside initWithCoder:. But if there are any additional parameters you would like to pass to the init method, you'll have to provide a way to configure them after initialization.

iOS: When will each initializer be used for table cells? init, initWithFrame, initWithCoder, initWithStyle

I recently did a programmatic alloc/init of a table cell subclass, and with some NSLog's, I was able to learn that pretty much all of the initializers were being called even though all I did was alloc/init, if I recall correctly.
init
initWithStyle
What is the logic to this?
Which one calls which?
Everything traces back to init. A UITableViewCell is a subclass of NSObject, so it has an init method.
initWithFrame is deprecated, and has been for some time (since iOS 3). You shouldn't be using it.
It was replaced in iOS 3 with initWithStyle, which you use to indicate what style of cell you'd like to create.
initWithCoder is another NSObject method, part of the NSCoding protocol. Again, you can see it in UITableViewCell because it is a sub-class of NSObject. initWithCoder is used to unarchive an object (perhaps you have saved an object directly to a file, for example).
You can tell which calls which by looking at the order in which the log messages appear.
A common pattern in Objective-C code is to have a "designated initialiser", which actually creates and returns a configured instance of the class. All of the other initialisers call the designated initialiser with fixed values for some of the parameters that weren't specified by the caller, or provide further configuration once the designated initialiser has returned.

Resources