I'm sure this might be fairly easy but I'm unable to do it. I have a custom View xib
and on basis of either segment selection I want to show one of my two tableViews. When tableView xib is loaded with
_userProfileView = [[[NSBundle mainBundle] loadNibNamed:#"UserProfile" owner:_userProfileView
options:nil]firstObject];
tableViewDataSource and Delegate methods are called on CustomView not on UserProfileView.
Am I missing something here ?
Related
So, I want to create a reusable widget as a xib and subview that can appear on a set amount of specific screens. This widget will have three buttons, each with an Action.
I want to be able to handle these actions on multiple viewcontrollers.
So say ViewControllerA, ViewControllerD, and ViewControllerF can handle the three button events, each in their own way.
I've created the nib file. How do I import it into the specific viewcontrollers, and then how do I wire up those events?
EDIT: I know that I could potentially get outlets set up via a viewcontroller, but Apple states that UIViewController is for full-screen views only, and my widget is only taking up a small portion of the screen.
You have done correctly. And one thing is, In iOS it's not widget.It's a UIView.
(Sorry there may be any typo in my code.I have written myself in StackOverflow)
Follow Below Steps to finish it..
1) After you have created the xib for the view, then you need to have a UIView subclass files.. For example your xib name likes this CustomView.xib means then create a files like this CustomView.m and CustomView.h
2) In your CustomView.xib , You need to set the fileOwner as your CustomView.h.
3) In your CustomView.m file, there will be a method like initWithFrame: In that method you need to load your xib file like this
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:0];
UIView *currentView = [topLevelObjects objectAtIndex:0];
[self addSubView:currentView];
4) Almost over. In any of your view controller, you can use this xib like
CustomView *newSubView = [[CustomView alloc]initWithFrame:CGRectMake(0,0,55,67)];
[self.view addSubView:newSubView];
That's it.. Go on..
You have created a nib file but make sure that you have also created the .h and .m file that will be the controller for that nib file. You will have to look up how to implement delegate methods which your other view controllers can capture and act on in their own way. Here is a great tutorial to get you started on making custom classes with custom delegates: Link
I have MasterViewController.h and MasterViewController.m files and within those files I load a subview on top of part of that view when the a button is pressed. I want the functions of that subview (IBActions, etc.) to be in different files (ContactUs.h and ContactUs.m) so I can abstract this program and break it into smaller/simpler pieces.
How can I go about doing this? The .xib file is loaded in the MasterViewController.m with:
[[NSBundle mainBundle] loadNibNamed#"ContactUs_GeneralContact" owner:self options[NSDictionary dictionary]];
Create a new subclass of UIView (via File - New ) and move the methods to the new subclass.
open your xib in Interface Builder
in the inspector on the right select the identity inspector and insert your class name as custom class
All done.
I am not sure i have understood your question:)...What you can do is take two view controllers 1. MasterViewController
2. ContactUsViewController
and two Views
1. MasterView(MasterViewController's View) main view
2. ContactUsView(ContactUsController's View) subview which is loaded on top
Write what ever function u need
in ContactUsViewController
import it in MasterViewController , instantiate it when ever you want and load it where ever you want:):)
Thus Master and ContactUs are completely independent of each other..:)have a nice day
Ive a project close to doing everything I need it to do. Its got a Main page which has four buttons that allow you to choose an option. Then a tableview page is launched from those options and displays a parsed XML feed from a website. You can then select one of the options in the table to see a detail view of the item, enquire about it, etc.
My problem is I need to add more elements to the TableViewController page, other than the tableview itself. I want a customized back button (not the navigation controller standard) plus some labels, images, etc.
In the TableViewController xib, the tableview itself fills the page. I cant resize it to add more elements above it. I can add a 'view' window seemingly above the tableview and put things in it. But it seems to add the view to the tableview. This means that when I scroll the table, the other elements like new back button, scroll away as part of the table.
So I'm led to wonder whether I need this page not to be a tableviewcontroller, but a viewcontroller, with a tableview inside it, as well as my other view with buttons, etc. Is that the right way to go? But if thats the case, then how do I instantiate the tableviewcontroller within code? Because my page will not be of that type anymore - it will be just a viewcontroller. But in code Im making a tableviewcontroller .. slightly scared by that route tbh.
Any illumination on this would be much appreciated, as various searches have left me none the wiser. Thanks.
To customize it, this is the way to go:
Change your class to be a view controller instead, which implements the TableViewDelegate and TableViewData Source protocols.
In the view didLoad of you controller, create the table view, set its delegate, data source, and any other properties you wish and add it as a subview to your view.
tableView = [[[UITableView alloc] init] autorelease];
tableView.delegate = self;
tableView.dataSource = self;
// .. Other customization
[self.view addSubview:tableView];
I suggest doing this programatically rather than IB.
Instead of a UITableViewController, you want a UIViewController that has an IBOutlet UITableView. Drag and drop a UITableView component from Storyboard and hook it up, and position it as needed on the screen. This UIViewController should implement the UITableViewDelegate and UITableViewDataSource methods.
Edit: Remember that a UITableViewController is just a subclass of UIViewController so you don't really have to get rid of all your code. :) You only need to make minor tweaks to your header and main file, and change the Storyboard to be a UIViewController instead of UITableViewController as I mentioned above.
I have a scroll view with many elements that I had to build as a separate xib because Storyboards graphic interface was clipping it and made me impossible to work with.
The xib is built with interface builder, setting the file's owner graphically to the same view controller allow me to ctrl-click and link between buttons and methods in it.
The view is added like this in the viewDidLoad method:
UIView *w = [[[NSBundle mainBundle] loadNibNamed:#"MainView" owner:self options:nil] objectAtIndex:0];
[self.myView.subviewolder addSubview:w];
where myView is the main view and subviewHolder is UIScrollView the container, both of them are linked to the controller, and the subview get added and display just fine. Self is of course the view controller.
What seems to not responds are the actions in view controller linked to the UIButtons I have in the subviews. I have put some breakpoints but the flow is just not passing there.
What am I missing ?
thanks
The answer was really simple, what I did wrong is to declare subviewholder much more smaller in height than the added subviews.
I thought that subviewholder would act as a placeholder, but instead it was causing the subviews to be displayed correctly but 'clipped' for what concerning the user interaction, therefore the IBAction was never called.
is it possible to design a view in interfacebuilder and use it dynamically a few times?
the view structure is
- UIScrollView
- UIView
- UIImageView
- UILabel
now i want to dynamically create multiple instances of the UIView in a for loop with different UILabels and UIImageViews. I want to give them side-by-side in the imageview.
thanks + regards
Sure. Just call:
[[NSBundle mainBundle] loadNibNamed:#"myNibName" owner:self options:nil];
It returns an array that contains all top-level objects you have placed in the NIB file. You can then assign those objects to any of your instance variables/properties. See the documentation for details.