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.
Related
I'm trying to add custom properties to the UIView so I can use them among all UIView objects and all the UIView's subclasses, like UIImageView, UISlider ... etc
I've tried to use Category to do so, but it turns out I can't use instance variables in the Categories' properties. So, I canceled this solution.
I, also, have tried to use Inheritance to do so, as if I made UIView subclass and added all the properties that I want to. But in this case I do get my additional properties for all of my custom class instances, but I don't get them for any of the other UIView subclasses, like UIImageView.
I'm trying to figure it out but I couldn't.
At the end, I figured a solution to my problem. I'm not sure if it was the best one but it does work for me.
I've used a runtime feature of the Objective-C 2.0 which called: Associated Objects.
This feature gives me the ability to add custom properties to any class using Categories.
I found those resources helpful:
• http://nshipster.com/associated-objects/
• http://www.youtube.com/watch?v=VxUjilPe5Do
I'm currently following CS 193P, and I'm on lecture 2.
If you've followed this class before, I'm where we just added a label to output the number of flips.
Here's the screenshot of the view and controller (original code from the lecture):
I notice the instructor is changing the flipsLabel's text from inside the setter for flipCount. But, I think it's easier/ more intuitive to just send a message to the flipsLabel object whenever touchCardButton method is being called. Here's the screenshot after modification:
Can somebody explain the reason why the instructor wrote it that way? He said "And here's another great use of getters and setters, which is to keep UI in sync with a property"
I started my iOS developer career from CS193P as well about 2+ years ago.
Just like KudoCC said above, if you use your method to set flipCount in 10 different places, then you will have to set self.flipsLabel in 10 different places as well. So, your method will have more lines of code while the professor's way is using less lines of code.
I personally think that it is the art of programming. We have different ways to achieve the same thing in programming. But, the less code you use in programming is usually the better way.
The content of self.flipsLabel only depends on flipCount property.
You may change the value of flipCount at more than one place afterwards, and if you work as the instructor said, you needn't update the content of self.flipsLabel every time you change flipCount.
You are in a simple user case which may not matter how to implement it, but if you are in a complex user case, you may change the value of flipCount at 100 places, in you intuitive way, you must add 100 times [self.flipsLabel setText:[....]], if you forget to add in one place, a bug is born.
The purpose of his code is: Everytime you set a new value for flipCount property, the text of label also changed. You don't need to set label text again. Your code will be clear, and easy to modify after.
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,
Is there any possible way to detect every change on User Interface during runtime??
I'm trying to find all objects in the current app interface.
I'm trying to to get all nodes inspecting recursively the main Window, but, for example, how to know if the top viewcontroller changes or if it's added a uiview dynamically, or is presented a modalview??
The main objective is to have a library to do this..
Any idea, help?
Thanks!
You could write your own library based on this, using advanced Objective-C techniques. I do not recommend you to do this, since it mostly breaks MVC patterns on iOS. Depends on what do you want to use it for, maybe analytics?
So these are the options I believe, if you want to actively inspect UIView hierarchy. All options are pretty complicated though.
Swizzle methods such as addSubview and removeFromSuperview of UIView, so you could know when changes like that happens. Including the getters of frame and bounds, if you wish to know the position.
You could use KVO to watch properties such as: subviews, frame, bounds, superview to notice any changes. But at one point you would have to add the same object as the observer (could be singleton).
Decide for an interval that is fired by a NSTimer and go through the hierarchy recursively beginning at keyWindow on UIApplication. This would have a big performance impact though.
There may be other options, but these are the ones I believe to be the best choices.
I would like to subclass a uibutton and redeclare a couple properties, but i don't want to change anything else. namely, I want to keep the standard plain system image provided.
is this possible or an attempt to eat cake and have the cake it tomorrow?
Yes it's possible. If you want to redeclare use subclass if you want to add more functionality use Categories