How to make second tabbarItem to be selected as default in iPhone? - xcode

I have added a UITabBarController programatically (Search,Login) and I want to set second tabbaritem(Login) and its view to be selected when Login credentials are not correct.But I m unable to do it so ..Could not understand where I m going wrong ..?
Search *first = [[Search alloc] initWithNibName:#"Search" bundle:nil];
first.title=#"Search";
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray= [NSArray arrayWithObjects:first,second, nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
[tabBarController setSelectedIndex:2];
[self presentModalViewController:tabBarController animated:NO];
But here my SearchViewController is selected and displayed as default...Where i m going wrong..?

Error is in this line:
[tabBarController setSelectedIndex:2];
Counting starts not from 1 but from 0, so in your case you have to change it to:
[tabBarController setSelectedIndex:1];

Related

Call phone number from UIButton

I've searched through all the questions and can't seem to find my answer.
I have the following IBAction. This is crashing every time you tap on the phone number. I have gone back to the DB and formatted the phone numbers to 5551235555 instead of (555)-123-5555.
- (IBAction)callPhone:(UIButton *)sender{
Bar *items = self.detailItem;
NSURL *pn = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", items.barPhone]];
[[UIApplication sharedApplication] openURL:pn];
}
- (void)setCallButton:(UIButton *)callButton{
Bar *items = self.detailItem;
[callButton setTitle:items.barPhone
forState:UIControlStateNormal];
}
Any code guidance would be appriciated.
Bar *items = self.detailItem; is not initiated, so that is why it is returning nil. Try the following:
Bar *items = [[Bar alloc] init];
items = self.detailItem;
Or what you should have done is make items an ivar for this particular class. Then you can initiate items once and use it throughout your class.

Xcode: How To Make PickerView Start At A Certain Row When It Is First Displayed

Is there a way to make the PickerView in Xcode start at a default row? Right now, my PickerView starts at the first element in my array.
So for example, if I have 1 component with 30 rows, how can I make the PickerView start at row 15 when the user first sees the PickerView?
Thanks
use -[UIPickerView selectRow:inComponent:animated:] ... assuming you have one component, do the following:
UIPickerView *aPicker = [[[UIPickerView alloc] init] autorelease];
aPicker.delegate = self;
aPicker.dataSource = self;
aPicker.showsSelectionIndicator = YES;
[self.view addSubview:aPicker];
[aPicker selectRow:14 inComponent:0 animated:NO];
You have to assign a variable named as selectedrow which have default value 0 in your viewDidLoad.
i.e. int seletedrow=0;
After that do following, in the didSelectRow method of UIPickerView:
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
selectedrow=row;
}
And at last write following code where you are initialising PickerView:
_pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 43, 320, 480)];
_pickerView.delegate=self;
_pickerView.dataSource=self;
_pickerView.backgroundColor=[UIColor whiteColor];
[_pickerView selectRow:selectedrow inComponent:0 animated:NO];
[_pickerView setShowsSelectionIndicator:YES];

Setting the text of an NSTextField programmatically

I have a GUI built on IB (Xcode 4).
It has a Static Text field connected to an NSTextField. After reading the information from an XML file it's supposed to change the text to whatever it is coming from the XML
the .h is as follow:
IBOutlet NSTextField * DataBaseLocation;
the .m
NSMutableArray* DBLoc = [[NSMutableArray alloc] initWithCapacity:1];
NSXMLElement* root = [doc rootElement];
NSArray* DBarray = [root nodesForXPath:#"//DataBaseLocation" error:nil];
for(NSXMLElement* xmlElement in DBarray)
[DBLoc addObject:[xmlElement stringValue]];
NSString * DBLocationString = [DBLoc objectAtIndex:0];
[DataBaseLocation setStringValue:DBLocationString];
NSLog(#"DBLoc: %#", DBLoc);
The NSLog shows that DBLoc has the correct string, yet the Text Field is empty and never gets set.
yes, I checked the connections in IB.
Any ideas? thanks!
Found the answer.
I needed to initialize the NSXMLDocument with NSXMLDocumentTidyXML like:
NSXMLDocument* doc = [[NSXMLDocument alloc] initWithContentsOfURL: [NSURL fileURLWithPath:input] options:NSXMLDocumentTidyXML error:NULL];
You should print out DBLocationString instead of DBLoc to make sure it's not empty or in some corrupted format that can't be passed as a string value and go from there.

how to set TTTabBar column number

I implemented TTTabBar like this:
_tabBar = [[TTTabGrid alloc] initWithFrame:CGRectMake(0, 0, TTApplicationFrame().size.width-80, TT_ROW_HEIGHT) ];
_tabBar.backgroundColor = [UIColor clearColor];
_tabBar.tabItems = [NSArray arrayWithObjects:
[[[TTTabItem alloc] initWithTitle:#"aaa"] autorelease],
[[[TTTabItem alloc] initWithTitle:#"bbb"] autorelease],
nil];
//_tabBar.selectedTabIndex = 1;
[_tabBar sizeToFit];
self.navigationItem.titleView = _tabBar;
_tabBar is TTTabBar
but appears three column buttons:
How to set the column number, coz I just need two buttons here.
And, as you see in my code, I've set initWithTitle:#"aaa" but there is no any title on button. Anything wrong?
WHAT IF I JUST NEED LIKE THIS:
Two grid buttons
Did you try to set _tabBar.columnCount to 2?
But you are looking for UISegmentedControl with style set to BarStyle.

NSarray release

If i declare an NSArray with alloc & retain in single sentence then should i release the NSArray object twice (i.e. [arrayObject release] 2 times) ?
If you are creating an NSArray with an alloc and a retain on the same line then you are probably doing something wrong.
Objects are alloced with a retain count of +1, so there is no need to call retain on it as well.
To answer your question directly; yes, you do have to release it twice. Once because you created the object and once because you retained it. But I would question why you need to retain it an extra time in the first place.
You don't need to retain it. You already retain--or take ownership of--an object when you alloc/init. Revisit the Memory Management Programming Guide for Cocoa.
No, you have to release the object for each alloc and each retain. (And you can't alloc an object more than 1 time anyway.)
If you do
NSArray* arrayObject;
arrayObject = [[NSArray alloc] init];
arrayObject = [[NSArray alloc] init];
...
then it just wrong code. The latter assignment will cover the old one, which causes a leak. Either use 2 objects, and release each of them once:
NSArray* arrayObject1, arrayObject2;
arrayObject1 = [[NSArray alloc] init];
arrayObject2 = [[NSArray alloc] init];
...
[arrayObject1 release];
[arrayObject2 release];
or release the object before another init.
NSArray* arrayObject;
arrayObject = [[NSArray alloc] init];
...
[arrayObject release];
arrayObject = [[NSArray alloc] init];
...
[arrayObject release];

Resources