UITabbarController default tab highlighted - objective-c

So I'm attempting to deal with uitabbarcontroller and wanted to have a default tab be selected when the view loads.
I have tried setting the
#property (nonatomic) int selectedIndex;
however this just sets which viewcontroller is being shown, not the actual tabbar being selected
I also tried this line
[self tabBar].selectedItem = [[[self tabBar] items] objectAtIndex:0];
but it crashes as you are not allowed to mutate the tabbar of a tabbarcontroller
Any help with this?
Thanks

So, as discussed in the comments, following code should do what you want:
[self.tabBarController setSelectedIndex:desiredIndex];
Alternatively, you can use:
[self.tabBarController setSelectedViewController:desiredViewController];

Related

Xcode: Create a group of outlets

This is just a quick question, I have lots of outlets in my code that need to be hidden initially and I want to make it so that in my viewDidLoad I only have to say something along the lines of colourObjects.hidden = YES; rather than individually going through and declaring if they are hidden or not i.e. redColourObject.hidden = YES;
blueColourObjects.hidden = YES;
greenColourObjects.hidden = YES; I would find it very grateful to know if this is possible and how you do it!
Thanks for any help
Hugh
IBOutletCollection is what you need:
#property (nonatomic, strong) IBOutletCollection(UIView) NSArray *stuff;
You can drag as many outlets as you want in it and they'll be there. You can also keep the original references for other purposes. then
for (UIView *view in self.stuff) {
[view setHidden:YES];
}
Sorry but there is not a way to do that. You would have to declare that individually.
What you could do I put them all into one UIView and then hide the UIView which would hide everything in in the UIView.

NSTableView without NSScrollView

I'm thinking to make un-scrollable nstableview.
Is it possible to have a table view without scroll view, or maybe to extract table view from scroll view ?
thanks
I don't know if this is good practice (I'm googling about that - that's how I found your question)(Edit: why wouldn't it be?), but it is possible to get an NSTableView without the NSScrollView: drag out a 'custom view' and set it's identity to NSTableView. There you are! The only thing is you don't get any visual feedback from IB about the tableview. But you can still set outlets (the delegate, datasource etc) in IB. I am afraid you'll have to set UI options (things like 'draws background' and such) from code (at least they don't appear if you use my 'trick').
You could even do it without IB (I found this code on SO, I didn't test it):
MyDataSource *dataSource = [[MyDataSource alloc] init];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:#"onlyColumn"];
NSTableView *table = [[NSTableView alloc] initWithFrame: frameWhereTableViewShouldGo];
[table setDataSource:dataSource];
[table addTableColumn:column];
[theViewYouWantATableViewIn addSubview:table];
At least you can implement your NSScrollView-subclass and reimplement its scrollWheel: method.
#interface MyScrollView : NSScrollView
#property (nonatomic) BOOL scrollingEnabled;
#end
#implementation
- (void)scrollWheel:(NSEvent *)event
{
if (self.scrollingEnabled)
[super scrollWheel:event];
}
#end
Did you mean this?
self.tableView.scrollEnabled=NO;

Objective-C: Adding UIButtons programmatically from another class

I'm having trouble connecting the dots between code and .xib files.
If I want to add a series of UIButtons programmatically to my view controller, how would I go about doing this from a separate class?
For instance, if I have MainViewController.m, which is set as the root view controller in Xcode, how can I add a UIButton to that view controller from SecondViewController.m? Is this even possible?
I would essentially like to place all of my "user interface" code in a separate class.
Thanks
To do this, create a UIButton *myButton programmatically and then call [mainViewController addSubview:myButton];. This may mean you need to store a MainViewController * property in your SecondViewController class.
Important methods and properties for a UIButton instance (essentially, just take a look at the documentation, but here's a minimal set of stuff to get you started):
+[UIButton buttonWithType:buttonType] - Make sure if you're doing anything remotely custom to use UIButtonTypeCustom here (it doesn't give you any default background images or otherwise to have to nil out)
setFrame: - Position the button relative to its container and set the size, for usability reasons the width and height should be at least 44 pixels (as mentioned here).
setTitle:forState: - UIControlStateNormal will act as the default properties for other states too, so you may only need to set the text here
setBackgroundImage:forState: - use UIControlStateNormal and UIControlStateHighlighted/UIControlStateSelected primarily, UIControlStateDisabled if you wish to show it grayed out or inaccessible at any point.
setImage:forState: - Use for an icon next to the button text (like an arrow pointing down for Save or up for Load, etc)
setEnabled:, setHidden:, setSelected: - Transition between different button states. setHighlighted: happens automatically when you tap the button.
addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside - TouchUpInside is almost always what you want for a simple button press, I'm using a method named buttonClicked: here to handle my button press.
Oh, and if you use [[UIButton alloc] initWith...] don't forget to [myButton release] once it's added to the mainViewController :)
use this
#import "MainViewController.h"
#interface SecondViewController
{
MainViewController *mainView;
}
#property(nonatomic, retain) MainViewController *mainView;
-(void)addButtons;
In your implementation
#synthesize mainView;
-(void)addButtons
{
UIButton *add = [UIButton alloc] init];
//do necessary stuff on button here
[self.mainView addSubview:add];
[add release];
}
In your MainViewcontroller.m
#import "SecondViewController.h"
-(void)viewDidLoad
{
[self superViewDidLoad];
SecondViewController *second = [SecondViewController alloc] init];
second.mainView = self;
[second addButton];
[second release];
}

Getting value of a button label from a different ViewController

This seems to be quite trivial but somehow I can't get it to work. I have 2 Popover Controllers with a tableView as contents, and I have a UIViewController with some buttons.
I'm setting one of the button's title from the PopoverController's didSelectRoxAtIndexPath function. This works nicely. Now I would like to pass this button title to the second PopoverController but can't seem to get this to work; the string returns a NULL. Currently I'm using:
h file:
NSString *myString;
#property(nonatomic,retain)NSString *myString;
m file:
#synthesize myString;
in viewDidLoad:
MyAppViewController *appV = [[MyAppViewController alloc]initWithNibName:#"MyAppViewController" bundle:nil];
self.myString = appV.buttonOne.currenTitle;
Try setting setting this property before displaying the second UIPopoverController.

UIViewController parentViewController access properties

I know this question has been asked several times and I did read existing posts on this topic but I still need help.
I have 2 UIViewControllers - parent and child. I display the child UIViewController using the presentModalViewController as below:
ChildController *child =
[[ChildController alloc] initWithNibName:#"ChildView" bundle:nil];
[self presentModalViewController:child animated:YES];
[child release];
The child view has a UIPickerView. When user selects an item from UIPickerView and clicks done, I have to dismiss the modal view and display the selected item on a UITextField in the parent view.
In child's button click delegate, I do the following:
ParentController *parent =
(ParentController *)[self.navigationController parentViewController];
[parent.myTextField setText:selectedText];
[self dismissModalViewControllerAnimated:YES];
Everything works without errors. But I don't know how to load the parent view so that it displays the updated UITextField.
I tried
[parent reloadInputViews];
doesn' work. Please help.
Delegation is the way to go. I know some people that may be looking for an easier solution but trust me I have tried others and nothing works better than delegation. So anyone having the same problem, go read up on delegation and follow it step by step.
In your subviewcontroller.h - declare a protocol and declare delegate mthods in it.
#protocol myDelegate
-(void)clickedButton:(subviewcontroller *)subController;
#end
In your subviewcontroller.h, within #interface:
id<myDelegate> delegate;
#property (nonatomic, assign) id<myDelegate> delegate;
NSString *data;
-(NSString *)getData;
In your subviewcontroller.m, synthesize myDelegate. Add the following code to where you want to notify your parentviewcontroller that the subview is done doing whatever it is supposed to do:
[delegate clickedButton:self];
and then handle getData to return whatever data you want to send to your parentviewcontroller
In your parentviewcontroller.h, import subviewcontroller.h and use it's delegate
#import "subviewcontroller.h"
#interface parentviewcontroller : VUIViewController <myDelegate>
{}
In your parentviewcontroller.m, implement the delegate method
- (void)clickedButton:(subviewcontroller *)subcontroller
{
NSString *myData = [subcontroller getData];
[self dimissModalViewControllerAnimated:YES];
[self reloadInputViews];
}
Don't forget memory management!
If a low-memory warning comes in during your modal view's display, the parent's view will be unloaded. Then parent.myTextField is no longer referring to the right text field until the view is reloaded. You can force a reload of the view just by calling parent.view;
However, a better idea might be to have the parent view have a String property that can be set by the child view. Then, when the parent view reappears, put that data into the text field, inside viewWillAppear: for example. You'd want to have the value set to some default value for when the parent view initially shows up too.
-(void) viewWillAppear:(BOOL) animated doesn't get called for me either, exactly when it's a modal view controller. No idea why. Not incorrectly overridden anywhere in this app, and the same problem occurs on the other 2 apps I'm working on. I really don't think it works.
I've used the delegate approach before, but I think that following approach is pretty good as well.
I work around this by adding a private category to UIViewController, like so:
.h file:
#interface UIViewController(Extras)
// returns true if this view was presented via presentModalViewController:animated:, false otherwise.
#property(readonly) BOOL isModal;
// Just like the regular dismissModalViewController, but actually calls viewWillAppear: on the parent, which hasn't been working for me, ever, for modal dialogs.
- (void)dismissModal: (BOOL) animated;
#end
and .m file:
#implementation UIView(Extras)
-(BOOL) isModal
{
return self == self.parentViewController.modalViewController;
}
- (void)dismissModal: (BOOL) animated
{
[self.parentViewController viewWillAppear: animated];
[self dismissModalViewControllerAnimated: animated];
}
#end
which I can now call like this when I want to dismiss the dialog box:
// If presented as a modal view, dismiss yourself.
if(self.isModal)
[self dismissModal: YES];
and now viewWillAppear is correctly called.
And yes, I'm donating a bonus 'isModal' property, so that the modal view can tell how it was being presented, and dismiss itself appropriately.