Can't find Storyboard ID in Identity inspector - objective-c

I'm trying to find the "Storyboard ID" which should be before Restoration ID in Identity inspector, but I can't find that for any view I've selected. I've tried to open a new project and I still can't find it either.
I'm using Xcode 5.0.2 developing for iOS-7.
Can you tell me what I'm missing here?

You are pointing to a UIView or some other object on the StoryBoard. Press the yellow indicator on top of the other objects which is your ViewController

The storyboard ID is a string field that you can use to create new UIViewController (not UIButton or other elements). An example of how to use it can be found here.

only View Controller has Storyboard ID
other view (eg: UIButton) no Storyboard ID
how get id of a normal view (in Storyboard) ?
two method:
use Referencing Outlet = normally is weak reference ~= like pointer in C
eg: add reference for UIButton
in Storyboard -> choose your UIButton -> Right Click UIButton -> Referencing Outlets->New Referencing Outlet -> drag it to ViewController.h -> got notice Insert Outlet -> popup window -> set Name to you want, eg deviceNameBtn -> Connect -> auto generated code into ViewController.h
#property (weak, nonatomic) IBOutlet UIButton *deviceNameBtn;
then in you (Objective-C) code, you can use deviceNameBtn as the ID of UIButton, do whatever you want
use (int type) tag
eg: your UILabel view's tag = 1
get your view:
UILabel *label = (UILabel *)[self.view viewWithTag:1];

Related

(OSX/Cocoa) How to set controller for main window

I've created a new cocoa application using .xib files (not storyboarded, the app has to be backwards compatible with mavericks/mountain lion) and I would like to have a custom windowcontroller for the main window. Is this possible? I can't seem to find a way to connect the window to my desired custom controller. The window has a reference outlet in AppDelegate, however I need a custom NSWindowController for this window as it doesn't open on application launch. The application launches silently as a menu bar app and the main application is launched via button press in the drop down from the menu bar.
Is there a way to link a controller to the window in the interface builder? Or do I have to do something along the lines of :
wc = [[CustomWindowController alloc] initWithWindowNibName:#"Main"];
Thanks!
Yes, open up Utilities (the right panel) in Interface Builder, and at the bottom click on the Object Library (circle with square in it).
Search for Object (blue cube), and drag it into your Document Outline (the panel on the left inside of interface builder)
From there, select that object you just created, and change the Class in the Identity Inspector to be the window controller you want.
Finally you can go into the Connections Inspector and connect your window to the window outlet
I can't seem to find a way to connect the window to my desired custom
controller. The window has a reference outlet in AppDelegate, however
I need a custom NSWindowController for this window as it doesn't open
on application launch.
Another way:
1) Delete the window in MainMenu.xib. Delete the window property in AppDelegate.m--because you deleted the window, it is no longer relevant.
2) File>New>File>Cocoa Class. Enter a class name, e.g. MainWindowController; select "Subclass of: NSWindowController"; check "Also create .xib file for user interface".
3) Create an outlet in AppDelegate.m:
#import "AppDelegate.h"
#import "MainWindowController.h"
#interface AppDelegate ()
#property (strong, nonatomic) MainWindowController* windowController;
#end
4) Declare an action in AppDelegate.h:
#interface AppDelegate : NSObject <NSApplicationDelegate>
-(IBAction)launchWindow:(id)sender;
#end
and implement it in AppDelegate.m:
- (void)launchWindow:(id)sender {
[self setWindowController:[[MainWindowController alloc]
initWithWindowNibName:#"MainWindowController"]];
[[self windowController] showWindow:nil];
}
5) In MainMenu.xib, hook up the Menu Item to the launchWindow() action: control drag from the Menu Item to the AppDelegate object and select launchWindow.
Create the controller and make it extend from NSWindowController.
In your xib file select the File's Owner and set it to your custom class.
Select your NSWindow and connect it to the File's Owner.
To open the window:
In your .h:
#property (strong, nonatomic) YourWindowController *yourWinController;
In your .m:
self.yourWinController = [[YourWindowController alloc] initWithWindowNibName:#"YourWindowController"];
[self.yourWinController showWindow: nil];

Can't change UILabel's text from another View Controller

I have two view controllers, one with a text field and a 'Next' button and another one with just a label. The idea is that you fill in your name in the text field and click 'Next'. It should then switch to the other view and the label should show your name.
When I switch views however, the label is just empty. I am rather new to Objective-C and I'm hoping someone knows why this is happening :).
ViewController.m:
#interface ViewController () {
IBOutlet UILabel *label;
IBOutlet UITextField *textField;
}
-(IBAction)go:(id)sender
{
label.text = textField.text;
}
-(IBAction)remove:(id)sender {
[sender resignFirstResponder];
}
You said that you have two View Controllers. The first View Controller has an instance of UITextField, and a UIButton. Your second View Controller just has an instance of UILabel.
However, I noticed in your code that ViewController.m, which I am assuming is your first View Controller, has two IBOutlets, one for a UILabel and one for a UITextField, which doesn't make sense because the UILabel is supposed to be part of your second View Controller.
What you need to do is properly delete the IBOutlet for the UILabel from your first View Controller. Then, in your second View Controller, add the IBOutlet for it's UILabel.
Then, you need to implement the prepareForSegue method in your first View Controller and add a code statement like this:
[[segue destinationViewController]label].text = textField.text;
This way, when the segue is performed, you will successfully pass your first View Controller's text field's text to your second View Controller's label.

Open 3 views on 3 buttons

I have 3 buttons on same page but those buttons are calling 3 different views on
click.Like button1 opens view1,button2 open view2 and button3 open view3.but i want all
those buttons on upside of view like tab bar is having downside.I tried by creating or
calling views on button click but on every click it's creating new object of view which i
don't want..Please help me to solve this issue.Is there any other way to solve this
problem. I tried segment too but not working.
I'm not quite sure, if this is what you really want.
The view would be larger than it actually requires to be, regarding your screenshot?
Why don't you just set the views size to match the size of the view that is below your three buttons.
In case the entire ViewControllers' view is of height: 524px
substracting the orange bar and the buttons: around 450px
Now just create the view from your buttons click action with the exact frame size, as your tableview and add it to the view as subview?
If another button is clicked, remove this view again and add the new view?
So in case you got this in one single ViewController, which I guess:
Simply change the following for your buttons:
So, in case you got the following 3 buttons
#property (weak, nonatomic) IBOutlet UIButton *buttonA;
#property (weak, nonatomic) IBOutlet UIButton *buttonB;
#property (weak, nonatomic) IBOutlet UIButton *buttonC;
Implement the function, which reacts on these three button events:
- (IBAction)myButtonAction:(UIButton *)sender
{
UIViewController *viewControllerToDisplay;
// Load UIViewController for its view from your .xib-file:
// Alternatively you could do this with switch.case and checking a button's tag-values, but I think is easier to read
if (sender == self.buttonA) viewControllerToDisplay = [ViewControllerA new];
else if (sender == self.buttonB) viewControllerToDisplay = [ViewControllerB new];
else if (sender == self.buttonC) viewControllerToDisplay = [ViewControllerC new];
// Only display the view, if the viewController was created
if (viewControllerToDisplay)
{
viewControllerToDisplay.view.frame = self.tableView.frame;
[self.view addSubview:viewControllerToDisplay];
}
}

In iOS, how do I reference an object in a view that is created with a xib file?

I have a view controller that is instantiated in appDelegate and pushed onto a navigation controller. The view controller's view is created with a xib file. The xib file puts a UILabel on the view (among other things). Now I need to set the label's text property programatically but I don't see how to get a reference to it. How do I reference the label object?
I'm using xcode 3.2.5 and building an iPad app.
Aside from IBOutlets, you can also set a tag property on the label in the IB. Then, when you need it you can do:
UILabel *label = (UILabel *)[self.view viewWithTag:111];
111 of course being the tag you assigned to the label in IB.
You do this with what's called an "outlet". You define them in your controller, mark them clearly as IBOutlet and then connect them in Interface Builder to your file owner (or other delegate object created in IB).
For instance, in your FooController.m you might have this:
#interface FooController ()
#property (nonatomic, weak) IBOutlet UILabel* fooLabel;
#end
Then you would select your label, and either control drag from it to the file owner, or go to its connections tab, and drag from the + under referencing outlet, to the file owner and select the fooLabel.
UPDATE: Code sample changed to reflect modern way of handling this case.
[self.view viewWithTag:NUMBER_OF_TAG]; does the trick. But remember that if you want to access the view you must do it on the viewWillAppear or viewDidAppear events.

XIB Displaying SubComponent

I created a custom view, which has one button and one text field , as given below
#interface CommUICustomSignInView : CommUICustomView {
IBOutlet NSButton *pBtn;
IBOutlet NSTextField *pTextField;
NSTrackingArea *pTrackingArea;
NSCursor *pPonitHandCursor;
}
#property (nonatomic,retain)IBOutlet NSButton *pBtn;
#property (nonatomic,retain)IBOutlet NSTextField *pTextField;
All items are linked properly, with the view,
In Another window controller XIB, i have added one tab view, in one of the tab item view, i am going to add this view,
added one tab view and assigning this view as below,
NSTabViewItem *pTabViewItem = [pTabView tabViewItemAtIndex:0];
if(pOfflineCTlist == nil){
pOfflineCTlist = [[CommUIOfflineCTlist alloc]
initWithNibName:#"CommUIOfflineViewController" bundle:nil];
}
[pTabViewItem setView : [pOfflineCTlist view]];
[pTabView selectTabViewItemAtIndex:0];
Now with that, i could able to track the mouse event in customSignInview, in nstrackregion,
but i couldn't see the other controls, sign-in button and text field,
Am i doing something wrong,
Finally i found following way to do this,
1 -- Created a Resource like Button / text field in the Main view,
2 -- Put them over the custom view,
3 -- Link them with the custom view,
now i could able to see it,
Is there any other approach then please suggest me,