How to connect to a nib from within StoryBoard? - objective-c

I have some code (ILColorPicker) that I want to install into my iPad app, which is using StoryBoards. The instructions from the author say:
In your view controller's XIB, add a UIView and then set it's class to ILColorPickerView
Wire the delegate property of the ILColorPickerView to your view controller in Interface Builder by selecting the UIView and clicking on the last tab. Then click on the circle next to "delegate" and drag that to "File's Owner" on the left.
Implement the delegate method: colorPicked:(UIColor *)color forPicker:(ILColorPickerView *)picker
How do I do this, when I am using Storyboards and there is no delegate or "File's Owner"? I have created a class with a subview to hold the picker, but see now that I don't need the subview. With that in mind, how do I connect this to my controller's view?

Files Owner is the View Controller. You don't need a Nib file for this, you can use a scene with a ViewController and just follow the directions from there. You can add a class (of type UIViewController) to you app and then associate that class with the VC in storyboard. That class will be the "Files Owner".

Related

iOS crash when pressing button from manually loaded viewcontroller

I programmatically instantiate a UIViewController from a xib, add it as a child VC to an existing main view, then add the child VC's view (which contains a button) to my main view. The view with the button displays just fine, but when I press the button, the app crashes with an "unrecognized selector" message.
I created the XIB by creating a custom subclass of a UIViewController ( File - New - Cocoa Touch class, selecting a subclass of UIViewController, and asking Xcode to also generate the XIB file). I then placed a button on that XIB, and hooked it up to the newly created UIViewController subclass. The XIB file's owner is set to the name of the new class.
So it looks to me like the ButtonPushed action message doesn't get send to my custom UIVC, and is going instead to the generic UIViewController - which rightly doesn't understand the ButtonPushed message.
How do I make sure that the action message sent when the button is pressed in fact goes to my custom UIViewController?
This is the code in my awakeFromNib method for the main view (main view is from Main.storyboard):
UIViewController *vc2 = [[UIViewController alloc] initWithNibName:#"ButtonVC" bundle:nil];
vc2.view.frame = CGRectMake(90, 140, 50, 200); //dont obscure the main view completely.
[self addChildViewController:vc2];
[self.view addSubview:vc2.view]; //vc2.view has a button, which shows up fine
[vc2 didMoveToParentViewController:self];
I've managed to get the equivalent functionality by creating a storyboard just with the button, and programmatically loading that, then instantiating my buttonViewController, adding it and its view as a child VC to my main view. That works just fine, but it seems to me like I should be able to achieve this with "only" a XIB.
Try this:
UIViewController *vc2 = [[ButtonVC alloc] initWithNibName:#"ButtonVC" bundle:nil];
And also make sure that the class in your xib for the view controller is set properly (see my answer here)

UIImageView - class never called

I'm using StoryBoard to create my app.
I created a custom class of UIImageView. In StoryBoard I added a UIImageView in my view and linked it to my custom class.
My class is never called and my imageview is not displayed. Did i miss something? Thanks.

UITableView on DetailView of SplitView doesn't load it's data

Using Xcode 4.3.1, ipad app for iOS 5.1, no core-data
On my storyboard I have a master-detail project. The master obviously has a TableView, and that works fine: by selecting an item in the master tableview my detail data is set in the detail view controller by the template code call setDetailItem (which I call from 'didSelectRowAtIndexPath'). That works fine.
My DetailView/Scene contains (another) UITableView (amongst other, also some TextFields etc.)
This UITAbleView's datasource and delegate are connected to the detail view controller.
In the detail viewcontroller's interface file I adde the protocols UITableViewDataSource and UITableViewDelegate (next to the pre-configured UISplitViewControllerDelegate).
I implemented the methods numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath, didSelectRowAtIndexPath methods in the detail view controller.
However, these are only called when the master tableview's list is build up, not when I select an item in the master list/tableview. When I select an item in the master list/tableview the detail data get's set in the detail viewcontroller, but for instance the 'cellForRowAtIndexPath' in the detail controller is never called, and the detail TableView is not updated/initialised. (The textfield in the detail view however is loaded with the correct data).
I guess I am missing something. Do I have to give the tableview a kick at the point I prepared it's data? And if so, how...? The tableview itself is not connected to an outlet or action, should it?
Thanks!
You need to implement a custom delegate (one-to-one communication) from RootView to Detailview.
Like the following,
Implement the Delegate in RootView,
#protocol RootViewControllerDelegate <NSObject>
-(void)didchangeSelectionInRootViewList:(id)toItem;
#end
And call the delegate when needed as (in your case on table view didSelectRowAtIndexPath),
//a delegete need in caes of splitview to refresh the detailview.
-(void)callDelegate:(id)sender {
if(self.delegate && [self.delegate respondsToSelector:#selector(didchangeSelectionInRootViewList:)]) {
[self.delegate didchangeSelectionInRootViewList:sender];
}
}
And don't forgot to set the delegate in Rootview as detailview while creating splitview controller.
And also catch the delegate in Detailview and reload the TableView with sufficient data to load.
thanks,
Naveen Shan

Add UIView on UIViewController

I want to add a "custom" uiview onto a uiviewcontroller, this custom view i created with xib and its a seperate from the view controller,
does anyone know how to add a uiview with a xib into a uiviewcontroller?
Many thanks in advance
You mean an additional view, not the main controller view? In that case you can declare a property for the view and load the NIB by hand:
#interface Controller {}
#property(retain) IBOutlet UIView *extraView;
#end
…
- (void) viewDidLoad // or anywhere else
{
[[NSBundle mainBundle] loadNibNamed:#"extras" owner:self options:nil];
NSAssert(extraView != nil, #"The extra view failed to load.");
[[self view] addSubview:extraView];
}
This assumes that you set the Controller as the file owner in the Interface Builder and you link the view to the extraView outlet. Also note that there might be more elegant solutions, like inserting the extra view into the main NIB for your controller; depends on the situation.
It looks like you want the most common scenario – simply load an intialized custom UIView subclass into a controller.
Create a new XIB, called “View XIB” in the Xcode new file wizard.
In the Interface Builder select the File Owner object and on the Object Identity tab in the Object Inspector (Cmd-4) enter Controller (or however your controller class is named) into the Class field.
Do the same with the view, entering the name of your view class.
Ctrl+drag from the file owner to the view, you should be able to connect the view to the view outlet defined on your controller.
Save, let’s say Controller.xib.
In your code, initialize the controller using initWithNibName:#"Controller" bundle:nil. The initialization code should load the interface for you and set the view property to the view unpacked from the interface file.
Go through some Interface Builder tutorial, IB is a very nice tool and it’s good to be familiar with it.

UINavigationController Push Views

Sorry - this may be an easy question, I'm new to iPhone development and still wrapping my head around Views vs ViewControllers.
I have a NavigationViewController and I can push Views using the following method in the RootViewController which is connected to a Bar Button Item:
- (IBAction)switch:(id)sender {
NSLog(#"Swith...");
LibraryViewController *varLibraryViewController = [[LibraryViewController alloc] initWithNibName:#"LibraryViewController" bundle:nil];
[[self navigationController] pushViewController:varLibraryViewController animated:YES];
}
I want to call this same method from a button on the same view that is currently loaded. Basically I want to have the Bar Button at the top call the same method as a button on the view. I was wondering how to call a method in the ViewController from the view loaded from that viewController. Hopefully that makes sense.
Do I need to create an instance of the RootViewController? I would think that would already be instantiated. Thank you.
BTW, the code you have pasted there is leaking your LibraryViewController. You need to either explicitly release it after pushing it, or autorelease it when it's created.
Your RootViewController should have its own xib file. In this xib, the RootViewController is represented by the object named "File's Owner". You can link buttons on the view to File's Owner the same way you can link things to RootViewController in MainMenu.xib.
You'll want to declare your method as an IBAction in your header file:
- (IBAction) myMethod: (id) sender;
Save your header, then switch to Interface Builder. Right click on the Bar Button, and drag from the selector tag to your view controller object (probably the File Owner). When you release, you should be given a popup menu of available actions, and myMethod should be selectable.
If you don't get this popup, you may need to make sure your File Owner class is set properly: select the File Owner in the file window, then select "Tools" > "Identity Inspector" from the menu. In the inspector, type your view controller's class into the Class field.