When I use the this method to initialize a UITabBarItem:
- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
Do I need to have a distinct tag for each tab bar item, or (since I don't use them) can I simply use the same tag value for all of them?
I'm pretty sure you can just leave them all as 0 or any other number you choose. Every UIView can potentially have a different tag, and Interface Builder sets them all to 0 by default. I haven't run into any problems with this.
From Apple's UITabBarItem class reference:
tag - The receiver’s tag, an integer that you can use to identify bar item objects in your application.
So it looks like it doesn't really matter.
Related
This question already has answers here:
Can you programmatically change the style of a UITableView after it's been created? [duplicate]
(3 answers)
Closed 9 years ago.
i have a table and i have also declared its style as grouped table style in view did load but i also have a button on click of which i want to change the style of the same table.
initially i was setting this in view did load method:
tableObj= [[UITableView alloc]initWithFrame:CGRectMake(5,50,310,300)style:UITableViewStyleGrouped];
but on button click event i am setting tableview fram and style see below code
tableObj.frame=CGRectMake(5,50,310,300);
tableObj style=UITableViewStylePlain;
but it gives an error.....assignment to readonly property ??
According to the docs, UITableView's style property is declared thus:
#property(nonatomic, readonly) UITableViewStyle style
That readonly keyword means that you can get the value of the property, but you can't set it. You can only set the style when you create the table using -initWithFrame:style:. This agrees with the error message you received:
assignment to readonly property
Put simply, you can't do that.
tblView.style;
It is a read-only property you cannot set any value to while you can only read it which is set.. You can check whether the property is changable or, not by writing your code as below
[tblView setStyle:];
But you will find that you can't do that, so you can't set.
Better you get 2 tableviews, or, reinitialize your existing tableview with different style.
It possible to change the frame of table view dynamically but you can not change its style
You set the table style when you initialize the table view (see
initWithFrame:style:). You cannot modify the style thereafter.
UITableView Class Reference
You cannot change the table view style like that. My advice use two tables or do something like this.
Initially it is like this:
tableObj= [[UITableView alloc]initWithFrame:CGRectMake(5,50,310,300)style:UITableViewStyleGrouped];
Then when you want to change do this:
[tableObj removeFromSuperView];
tableObj=nil;]
//if not using ARC
[tableObj release];
tableObj= [[UITableView alloc]initWithFrame:CGRectMake(5,50,310,300)style:UITableViewStylePlain];
[self.view addSubview:tableObj];
According to the UITableView Class Reference, you can not change the style of a UITableView
When you create a UITableView instance you must specify a table style,
and this style cannot be changed.
See this.
Can you programmatically change the style of a UITableView after it's been created?
No this is not possible See this is wriiten in apple documentation:-
How could I use 3 PickerViews in the same View vertically?
(I can't set height..)
Because I try so many times,But it's still no work.
THANKS...
I think you dont need three picker view in any case if you want to show three type items then you can use cthree components in single pickerview.use this datasource method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView for doing this.
In the IB under Identity tab you can find an attribute called "Object ID". I can not find a way to get hold of this ID from code. Oh, and I know about the tag attribute but it's not what I need.
I essentially would like to get the unique object ID for a UIComponent that was touched on the sceen. I already have the UITouch object.
The Object ID in Interface Builder is only an internal book-keeping value used by IB when deserializing/serializing XIB files, and does no longer exist when the Application runs.
You want to use tag, or alternately, a property/outlet.
For UIView I normally use the tag property.
- (IBAction) buttonPressedid) sender {
NSLog(#"tag: %i", [sender tag]);
}
I'm pretty sure you can set the tag property in IB :)
use tags instead of the IB Object ID. As far as I know this object ID is only used in interface builder.
You can set the tag in the Attributes tab.
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.
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.