maintaining selection in UISegmentedControl - iphone

Can i maintain the selected state of UISegmentViewControl segments??
i.e can keep a segment appear selected even if the user selects another segment??
I dont seem to find anything that does this anywhere!!

This isn't possible out of the box. (See How can I enable multiple segments of a UISegmentedControl to be selected?.)
You could try something like this code to provide similar functionality.

I found a way arround this.I placed dark colored image behind each segment and set their hidden property to true.Then i decresed the alpha value of the uisegmented control.Then in the code when a segment is clicked i turn the visibility on or off accordingly,so multiple segments appear selected

Another solution might be using a category:
#import <UIKit/UISegmentedControl.h>
#interface UISegmentedControl (MultiSelect)
#end
Doing this, you have in principle access to private member variables of UISegmentedControl . In particular, you have access to the array holding the button segments, which you can manipulate according to your needs by overriding setSelectedSegmentIndex:selectedSegmentIndex: .However, for various reasons, the attributes declared as private still shouldn't be accessed directly, see this link. As also suggested there, you can rather abuse KVC. So the implementation could look as follows:
#implementation UISegmentedControl (MultiSelect)
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
NSMutableArray *pArraySegments = [self valueForKey:#"segments"];
if ((pArraySegments) && (selectedSegmentIndex >= 0) && (selectedSegmentIndex < [pArraySegments count])) {
UIButton *pSegment = (UIButton*)[pArraySegments objectAtIndex:selectedSegmentIndex];
pSegment.selected ? (pSegment.selected = NO) : (pSegment.selected = YES);
}
}
#end
This works for me. However, since I now read this discussion, I'm not quite sure if this is really a valid approach.

Related

Update UILabel with Global NSString

In my delegate i have an NSInteger healthInt; and NSMutableString * healthString. In appDelegate.m i have set healthString to hold the changing value of healthInt. Then in a different view i have UILabel * healthLabel. And I have set healthLabel to display healthString with the following code.
healthLabel.text = [NSString stringWithFormat:#"%#", appDelegate.healthString];
This works and displays the number 100, which is what i have set healthInt to in the appDelegate. But when a UIImageView mainSprite collides with a different ImageView healthInt should decrease by two. Which it does, i can tell because i can see it happen in the log. The log may change and display the values of the slowly decreasing healthInt, but the healthLabel doesn't update as healthInt decreases. My question is how can i get this healthLabel to update as healthInt decreases? I have tried just putting in that code for the collision detection between mainSprite and the other ImageView that causes damage but that doesn't seem to work. Thanks!
If you want to get your UILabel to update every time healthInt or healthString changes, there's a couple ways to do this.
One way is to go into the setter method for healthInt (or create one instead of using #synthesize) and broadcast a NSNotification for each time your healthInt number changes. And then when the view with that label is visible, have an observer registered pick up on that notification and make a change to your label.
Another way is to use Key Value Coding & bind the UILabel's text field to healthString. Here is a related question that may help you use this possible solution.

1 tag multiple UILabels in xcode, iphone

So I have many labels in my app that contain the same text. I am localizing them, and would like to know if its possible for me to tag them all with the same number and assign the new text that way.
currently when I have tested it only recognizes the first label with the tag.
I really want to save myself from having all these repeat outlets
Yes,
You can tag them with a integer number because in inheritance tree NSObject is the super class to UIView,
And NSObject has an integer property called tag .
USE tag like below
myLabel1.tag = 1;
myLabel2.tag = 2;
...........
...........
myLabeln.tag = n;
It is a little inclear from your question what your problem is, but I am assuming from your question that you want to be able to update all your UILabels which are located at different places within your app with the same text.
You could create a global header file that contains an NSString which holds the text for all your labels, and then #include this header in all view controllers which contain labels which need to show this this text. Then whenever you update the NSString in the global header you update any labels that are currently in view or about to be viewed aswell. This can be done from within your IBAction methods, as well as implimenting the viewWillAppear:(BOOL)animated method. Whenever a view that contains relevent labels comes into view, it will apply the value of the NSString in your global header and update its labels.

pagecontrol indicator custom image instead of Default

How can i change the color of pagination dots of UIPageControl?
In this link the sample code is given.But it shows 3 errors for me...
1'st ERROR:
Line:CGRect currentBounds = self.bounds;
Error:requst for member 'bounds' in something not a structure or union
Method:-(void)drawRect:
2nd Err: same error with same line in touchesBegan method.
3rd Err:#protocol PageControlDelegate
#optional
(void)pageControlPageDidChange:(PageControl *)pageControl;
#end
Error:Expected ')' before 'PageControl' .These are the three errors occurs for me...Please help me out to solve this..
I want to change the pagecontrol indicator(dot) color...
Thanks & Regards,
Renuga
first error is probably due to the fact that self does not refer to a view (a view controller maybe)
Second error is because PageControl is not yet defined by the time the parser come to your protocol definition.
Typical Class with delegate
#protocol MyProtocol;
#interface myClassWithDelegate
{
id<MyProtocol> _delagate;
}
#end
#protocol MyProtocol
-(void)MyClass:(MyClassWithDelegate*)c says(NSString*)message;
#end
I'm the one who wrote the sample code you are using.
I see that VdesmedT has already helped you on the syntax issues you were having. So +1 for that!
As for customizing the dots: The class as provided does not support custom images for the dots. It just draws circles using Core Graphics. The color of the circles is configured using the properties dotColorCurrentPage and dotColorOtherPage.
The default colors are grey dots with a black dot for the current page (because that is what I needed when I wrote it).
Let's say you want a red dot instead of a black dot for the current page and green dots for the other pages. When you create your PageControl instance you simply assign the properties like this:
pageControl.dotColorCurrentPage = [UIColor redColor];
pageControl.dotColorOtherPage = [UIColor greenColor];
... assuming your instance variable is called pageControl. Or use any of the other convenience/initialization methods for creating a UIColor that you like.
Hope this helps.

Using tags in iphone programming--can someone explain?

I've seen people using tags in iphone programming, like inside labels or tableview cells:
name.tag = kNameTag
Can someone explain with an example how these tags might be used? I gather that it's so that you can refer to a ui element later? Like if you programmatically use a for loop to create an array of UIButtons onto the iphone screen, do you assign tags to each button within the for loop or something?
Thanks!
The example you've included in your question is one of the common ones.
You can instantiate buttons (or other UI elements) in a loop, assigning a incremental tag to each. When an IBAction is invoked by one of those buttons, you can ask the sender for it's tag, which tells you exactly which button triggered the request.
for( int i = 0; i < 10; i++ ) {
UIButton * button = [[UIButton alloc] init...];
button.tag = i;
}
IBAction:
- (IBAction)doSomethingFromButtonTap:(id)sender {
NSLog(#"Button pressed: %d", [sender tag]);
}
They're also widely used to find specific subviews within a parent view. UIView provides a viewWithTag:(NSInteger)tag method. This is useful when building custom views without subclassing (or situations where you don't want to hold references to subviews, but know the tag).
Tags are integers. You assign them to a view using UIView.tag. You then use -[UIView viewWithTag:] to search the view hierarchy for the view.
UIKit doesn't use tags (I think), so they're free for you to use as necessary. However, tags are global to your app, so they're not an ideal replacement for IBOutlet (but it's often more convenient when you have a lot of views).
Avoid using 0 as a tag, since it's the default tag — [v viewWithTag:0] is unlikely to return the view you're looking for.

UIView Hell. Hiding one subview hides them all

I am apparently in some swirling UIView hell zone at the moment where up is down sibling is parent and my brain is completely fried.
Here's the deal. Really, really simple. I have a container view with N leaf node sibling subviews. No tricks here, dead simple. I do the following:
// occludedPageSet is the set of view tags corresponding to views that are off screen and // thus fully occluded. This was determined geometrically.
for (NSNumber *n in occludedPageSet) {
// Point to a view corresponding to this tage
UIView *v = [self.containerView viewWithTag:[n integerValue]];
// Hide this view
if (v.hidden == NO) {
NSLog(#"View %d is occluded. Hide it.", [n integerValue]);
v.hidden = YES;
} // if (v.hidden == NO)
} // for (occludedPageSet)
Pretty tame stuff. Unfortunately ALL sibling views vanish! What the?!? How is this possible?
Do I need a [retain]/[release] for v here. I'm stumped.
Baffled,
Doug
Am I missing something about the problem here? It's only natural that if you hide a view, any view it holds as a subview would be hidden as well. After all, you can't see the container view...
If you put ten things in a box and make the box invisible, wouldn't you expect that to mean you couldn't see the things in the box? Similarly an invisibility cloak would be of little use if only the cloak were invisible and not the person beneath...
If you need some things visible and some not, work on the specific items and not the container.
Apparently, all of your views are included in occludedPageSet, or all of your tags are the same n.
NSNumber *n in occludedPageSet
Or, one of the v views is the parent of the rest, so when you hide it, you hide them all.
Make sure self.containerView's tag is something completely different from any of the children's tags. Calling viewWithTag will return the receiver if it is the given tag, which will in turn hide all of your views. Either step through the iteration or print out the address that v points to so that you know you're occluding what you should be occluding.