I use xib to create my custom view, and i bounded it with the same name .h .m file.
I set the custom view class to UIView's class in storyboard , but it didn't work.
my custom view
I drag a UIView
Step 1 :
Create a Xib (your View) file, then create your.h and .m files,
If you already have a viewcontroller that will use the xib: implements
everything in it.
Step 2 :
Open your Xib file, in the interface builder you will find on the left : File's owner, select it and then, on the right in the identity inspector tab, add your custom class.
Step 3 :
import your xib class in your.h file
Now you can create IBOUTLET in your.h file.
Step 4 :
To get the xib in your.m just write something like this -->
in viewDidLoad:(or everywhere you need it)
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"RunningMode_VC_Interface" owner:self options:nil];
runningViewXib = [subviewArray objectAtIndex:0];
runningViewXib.frame = self.view.frame;
[self.view addSubview:runningViewXib];
You will now add your custom view to your viewcontroller. Then create your custom interface/functionality methods to work easy on it.
hope it helps.
Related
I am using swift2.0 and I have just added a custom UIView called SortableTableTitleView:
and then I drag a UIView in my storyboard, and I changed the class of the view to SortableTableTitleView
When I tried to set the text of the label in the custom view, I just got a bad access error.
I tried to write the initwithcoder method of the view, but I don't know how I can load the element from xib. (It is somehow different with ObjectiveC). So, how can I solve this?
You cannot use xib files within the Storboard file. So either create the view fully in the Storyboard and use it as per normal. Or, add the view from your xib file add a subView to the view on the storyboard via code.
So something like this in the viewController:
UINib *nibFile = [UINib nibWithNibName:#"SortableTableTitleView" bundle:nil];
NSArray *views = [nibFile instantiateWithOwner:self options:nil];
UIView *sortableTableTitleView = views[0]; //Assuming it is the only view in the xib file
//Add it where you want as a normal view now:
[self.view addSubview: sortableTableTitleView];
I realise the code above is in objective-c - I'll get the Swift comparison when I get a gap (Being new to Swift and all)
I've tried this class
https://github.com/autresphere/ASDepthModal
i want it to popup like it does but i want to be able to set the labels programmatically, since i need the to change depending on what day it is.
I'm using storyboard, so i've created a .xib and uiview.h and uiview.m. In my main UIViewController i have:
xibContents = [[NSBundle mainBundle] loadNibNamed:#".xib" owner:self options:nil];
testView = [xibContents lastObject];
in my .xib i have set the file owner to my uiview class, this create a problem: NSUnknownKeyException
When i set the uiiew inside my .xib to my uiview class the application will load and i can open it just like it should, but i'm not able to change the state of the label programmatically? I'm complety lost here!
Typically speaking, UIViews do not have access to IBOutlets. Apple kind of intended xibs to only be assigned to UIViewControllers.
However, you can load a view from a xib in two ways:
1) Create an extra xib to use in your UIViewController. Set the File's Owner to your view controller, and the class name of the view to your custom view class. In interface builder, this is under "custom class". You can set the view as a IBOutlet, and iOS will create an instance of your custom class when your UIViewController loads the xib and sets itself as owner (like you tried above, but only from within a controller class)
2) Load a xib in a UIView class, and set self to the resultant object:
- (id)init {
self = [super initWithFrame:CGRectMake(0, 0, 1024, 352)];
if (self) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:#"TCSNumberKeyPad" owner:self options:nil];
[[nib objectAtIndex:0] setFrame:CGRectMake(0, 0, 1024, 352)];
self = [nib objectAtIndex:0];
}
return self;
}
In either case, you will need to retrieve your label via code rather than IBOutlet properties. You can find your label in the subviews property:
UILabel* myLabel = [self.subviews objectAtIndex:0];
Actually got this to work. I took the wrong approach. I found it simpler to just create the view and populate it with background image and labels when the button got clicked. would have been simple to do it in a UI designer, but this wasn't that hard actually.
Thanks to the people who helped me :)
The file's owner should be the view controller, not the view itself. The view can have outlets to the labels. The view should be set to your custom class in your nib.
I'm trying to modularize a complex UI in several xib's.
I want to programaticly load each additional xib from file.
The xib has controls which are connected to the custom view code.
Then I try to load the xib the following way inside the main view controller:
WIFClientData *clientDataView;
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"WIFClientData" owner:self options: nil];
clientDataView = [nibViews objectAtIndex:0];
[clientDataView configure];
The problem is that when I try to configure the view:
-(void)configure
{
self.name.label.text = #"Name";
self.mobile.label.text = #"Mobile";
}
name and mobile are nil
I currently don't have File's owner connect, or set to any custom class. I only have the view which is layed in the xib, set to the custom class.
I have been reading a lot of things regarding to this but haven't found the answer yet.
Can someone help me?
Nuno
If you have a custom class WIFClientData and set a top-level view's class to WIFClientData in nib (which you then get via [nibViews objectAtIndex:0]), have IBOutlets connected to that view (not to file owner) then this should work. (works for me for custom cells)
Maybe you've misaligned something or mistyped?
Upon a certain user action, I wish to add to my UIViewController another UIView that will be half transparent; i.e. when it loads, the UIViewController view in the back will still be visible in the background, and the new UIView will appear as a layer above it.
The "Half Transparent" UIView should have several images and buttons in it, so I prefer to create a separated h, m and xib files for it so I can control it.
How should I do that?
Try this:
UIView *view = [[UIView alloc] init];
[view setAlpha:0.5];
[mainview addSubview:view]
Subclass UIView, create the nib file
Change the nib class to your custom subclass name
Change the file owner to become your view controller
In your view controller, declare a #property for the custom view using IBOutlet
Select the nib, drag from the file owner to the custom view and connect the outlet
In your button action, when you are ready to load the view, use
[[NSBundle mainBundle] loadNibNamed:#"NibName" owner:self options:nil];
Once this is done, your custom will be loaded from the nib and assigned to the property you declared.
I'm new to iOS and Xcode.
I can't figure out how to design a separated view and make it be added into the main UIViewController using storyboard.
I did different approaches..
Just grab an UI object from right-bottom corner window in the xcode, and then put it onto any space area of storyboard. But I can't drop the UI object like the way with xib.
Add a new UIViewController. Add a view into the UIViewController. In the main ViewController.m, I get the new UIViewController instance in the viewDidLoad, and then [self.view addSubview:newUIViewController.view]. But I can't see the added view.
I created a new xib file. And add a view into it. I also try to get the instance in the main ViewController. And addSubview with the xib's view. But it also failed.
Is there a correct way or any working solution to do so?
I figured out a way to do it.
Described as following:
Create a .xib file. For example: MyView.xib
Create a objective-c class. For example: MyViewClass.h and MyViewClass.m
Set the .xib File's Owner to the class.
Add a UIView element on the storyboard and set the custom class to the objective-c class name (MyViewClass).
The key-point is to override the initWithCoder method in the object-c class.
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
The idea is the custom class is loaded by the storyboard and initWithCode will be called.
The index 0 is the root view in the .xib interface builder.
It's kind of tricky but it works.
In storyboard, drag and drop a view controller. The view controller come with a main view. Select that main view by clicking outside of the added view controller and then clicking in the center of it. Now, just drag a uiview or whatever into that main view just like you did with IB.