I'm creating custom control that behave slightly like a UISlider. I would like to allow objects using this class to be able to addTarget:atSelector:forControlEvent: the event would be here UIControlEventValueChanged.
How can I make my custom UISlider like object send the selector on the target upon value changed ?
Okay, just make your object a subclass of UIControl and you're good to go.
here is the tutorial link which is having sample code as well at the end.
http://www.raywenderlich.com/36288/how-to-make-a-custom-control
hope this will help you.
Related
I have a UIButton object in my program.
I want to use it like follows
myButton.setImage:blablabla
mybutton.title:.......blabla
...
...
myButton.placeTextBelowImageWithSpacing:12
While calling my method "placeTextBelowImageWithSpacing:12" it must set the image and text accordingly. I have the method ready with me. How can i use it in the above way.
PS: I hate subclassing.
Thanks in Advance
Create a custom subclass of UIButton. I created a button called FinderButton that has an image and a title centered below it. It works great.
If you hate subclassing then you might want to think about a different line of work.
Being an Objective C programmer that hates subclassing is a bit like being a surgeon who hates blood or a farmer who hates dirt. Defining a class hierarchy is one of the main tools for doing development in an OO language like Objective-C.
You can do this by creating a UIButton category:
UIButton+MyCustomMethod.h
#interface UIButton (MyCustomMethod)
- (void)placeTextBelowImageWithSpacing;
#end
UIButton+MyCustomMethod.m
#implementation UIButton (MyCustomMethod)
- (void)placeTextBelowImageWithSpacing
{
// ...
}
#end
You can't. That isn't valid syntax in Objective-C. The closest you can get to that would be to explicitly declare new properties on UIButton that followed your naming convention. Using them would then look like:
myButton.setTitle = #"something"
Then you could override setTitle's setter (setSetTitle:), and making it call setTitle:forControlState:, which I'm assuming is your goal.
But this should only be done through subclassing (learn to love it, it's a big part of OOP), although if you really really want to, you can add the properties in a category using the Objective-C runtime objc_setAssociatedObject() function more info here: Objective-C: Property / instance variable in category
I have a custom singleton MyColors class to easily change colors of some buttons easily in many UIViewControllers at the same time.
I put #import "MyColors.h" in every class I need to get color of a button.
Everything works perfectly and setting a the color value in MyColors, that color is applied correctly to where I want.
Now, I want to change colors alive. So I send a NSNotification with name "COLORS_CHANGED".
So every class will observe that and apply colors immediately.
What I want to do is:
Force XCode to remind me that the .mfile that includes MyColors.h must also include colorsChanged method for observing. So, this will prevent me forgetting to add observer
OR:
Add something like a Category or something like that into MyColors.h to do this kind of thing...
What I don't want to do:
I don't want to create a protocol and add it as delegate to header files of every .mfile that includes MyColors.h. Because I can forget that too.. If not, this would be the way ofcourse...
So, I am trying to find an Objective-C way to do this, or a compiler way that works with XCode to do this.
Thank you for sharing your ideas.
Update: I use color values for anything that needs colors not just buttons.
implement this functionality in a base class and make all your UIViewController-s inherit it. i.e - in the base class register for this notification and change the common objects' colors and also call there a virtual method for the inheritors to change their extra objects' colors.
Good luck,
i asked me whether it is possible if i can create a uislider who has 2 handles to select a range. Just like here:
The problem i am facing is that i dont want to use a custom UIControl Subclass. I need a UISlider subclass or a other solution for this problem, because a lot of the code is based on UISlider specific propertys etc. So is there any possibility to achieve this ?
Look at the following example:
http://www.cocoacontrols.com/platforms/ios/controls/rangeslider
You can subclass UISlider, but it will be very difficult. Your class should offer quite some new properties, and the old ones won't make much sense at all.
Not sure how your code can be based much on UISlider specific things - as everything would change the meaning (i.e. ranges instead of one value).
If you really need a common base class, you could encapsulate ("has-a" relationship) the control in a custom class and let this handle the different types.
I implemented a similar control using a custom view, and it happened to be quite straight forward.
UISlider doesn't provide the functionality you're after, and subclassing UISlider probably won't work out. What would the value of such a control be? The value of a slider is a number, but you want it to be a range. Consider a custom control that duplicates the UISlider properties you need.
I have derived my own View class from UIView that handles gestures and drawing itself.
I use Interface Builder to place several instances of it on a View.
On certain events, I want to call several delegates in the UIViewController, just like an UIButton::onTouchUpInside event. I don't want to set up an interface protocol and connect an IBOutled id instance, like in (1).
I was looking all around documentation and also stack overflow, but I haven't found any clue about the syntax.
So, what is the syntax for that with Xcode 4.4 (just updated)?
Deployment Target will be IOS >=5.0 because of the custom properties I already use.
[EDIT]
Subclassing from UIControl does give indeed access to the standard UI events like TouchUpInside, but is it possible to add custom named events like "onSomethingElse"?
(1) Events for custom UIView
I'm not entirely sure exactly what you want from this view, but if you want to handle things like UIButton::onTouchUpInside event, then maybe you should look into subclassing UIControl instead of UIView. It gives you access to events, just like UIButton.
If you post a NSNotification of whatever custom event you define, you can have multiple listeners registered with the NSNotificationCenter to respond to the event.
You can see details by looking for addObserver:selector:name:object: and postNotificationName:object:.
is it possible to click UIButton and to call its action through Programming without clicking?
You can just simply call the method associated with your UIButton action as follow :
- (IBAction)myMethod:(id)sender {
// some code here
}
When you want to fire an action :
[self myMethod:nil];
Yes you can do it programmatically just send a event to button
Well I guess you can get a little help from a friend of mine that help me a lot to get start with objective C and Iphone development Basics. maybe you already have seen him but if not here is the link http://www.youtube.com/watch?v=B7jB0D3Y7Ws&feature=channel pretty easy to understand.