I'm trying to implement something just like the HeaderFooter sample code that apple provides:
Unfortunately they used Interface Builder to do most of the work here, and I'm beginning to see that interface builder is the hardest thing to deal with in iPhone development.
My table works fine, but the header and footer aren't there at all. My question is how can I make it work.
My code is identical to Apple's, with a few name changes:
- (void)viewDidLoad
{
// headerView
CGRect newFrame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, self.headerView.frame.size.height);
self.headerView.backgroundColor = [UIColor clearColor];
self.headerView.frame = newFrame;
self.tableView.tableHeaderView = self.headerView; // note this will override UITableView's 'sectionHeaderHeight' property
// set up the table's footer view based on our UIView 'myFooterView' outlet
newFrame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, self.footerView.frame.size.height);
self.footerView.backgroundColor = [UIColor clearColor];
self.footerView.frame = newFrame;
self.tableView.tableFooterView = self.footerView; // note this will override UITableView's 'sectionFooterHeight' property
//...
The problem must be in my xib file, but the sample file has one giant nib that defines everything, all the way back to the window so I can't use that. I had to make my own.
I started with a uitableviewcontroller and nib and added header and footer views views to the nib.
I connected them to their respective IBOutlets in the table view controller subclass.
The delegate and datasource are connected and all the table cells work fine.
What did I forget? Why isn't it making the header and footer?
Follow up question:
How can I learn how to use IB? I try to avoid it, but sometimes I can't. It seems really poorly documented.
It worked when I used initWithNibNamed: to create the view. I'm not really sure why. It seems to use the .nib when I use initWithStyle:. I have to admit, I'm still fuzzy on the relationship between UIViewControllers and .nibs…
Related
I am using an NSViewController to load a nib with a view that renders a Core-Plot chart. I replace the "target view" using the following code:
NSViewController* aViewController = [[NSViewController alloc] initWithNibName:#"nib name" bundle:nil];
if (aViewController != nil)
{
myCurrentViewController = aViewController;
}
// embed the current view to our host view
[myTargetView addSubview: [myCurrentViewController view]];
[[myCurrentViewController view] setFrame: [myTargetView bounds]];
This results in basically everything in the chart looking slightly fuzzy. I commented out the setFrame and also tried integer values for the setFrame and it made no difference. The target view also does not have layering on.
If I get rid of the view controller and change myTargetView to the actual chart view class(PlotView) then it is no longer fuzzy. This is on 10.8. Any idea why using the NSViewController route is messing up the chart view?
Update #1: I am using from the Plot_Gallery_Mac sample project the PlotGalleryController, PlotView, PlotItem, and VerticalBarChart to generate the chart through an NSViewController. I am guessing the setup in the sample PlotItem class (particularly the renderInView method) is not compatible with using it with an NSViewController.
I have a UIViewController with an xib and using Interface Builder I've added a child UIView.
Within the child UIView, when I click on an object within that view, I want to be able to alter the title of the whole window.
Now I'd normally do that setting
self.title = #"hi";
on the parent UIViewController. But is there any way I can access the parent title from within the child?
I've tried
self.superview.title = #"i";
self.parentViewController.title = #"hi";
but neither work.
Any help much appreciated
thanks
self.superview.title = #"i"; evaluates to an object of type UIView, and UIView has no title property. UIViewControllers have a parentViewController property but UIViews don't.
So the fundamental problem is that you're not properly separating your controller and your view classes. What you'd normally do is make the view you want to catch taps on a subclass of UIControl (which things like UIButton already are, but if it's a custom UIView subclass then you can just change it into a UIControl subclass since UIControl is itself a subclass of UIView), then in your controller add something like:
- (void)viewDidLoad
{
[super viewDidLoad];
// we'll want to know if the view we care about is tapped;
// we've probably set up an IBOutlet to it but any way of
// getting to it is fine
[interestingView
addTarget:self
action:#selector(viewTapped:)
forControlEvents:UIControlEventTouchDown];
// UIButtons use UIControlEventTouchUpInside rather than
// touch down if wired up in the interface builder. Pick
// one based on the sort of interaction you want
}
// so now this is exactly like an IBAction
- (void)viewTapped:(id)sender
{
self.title = #"My new title";
}
So you explicitly don't invest the view with any knowledge about its position within the view hierarchy or how your view controllers intend to act. You just tell it to give you a shout out if it receives a user interaction.
This has been asked before, but I cannot find a definitive answer.
I would like to design a custom UIView class. I would like to do the layout in XCode 4 in a XIB file. Ideally:
I have the files MyView.h, MyView.m and MyView.xib.
The code defines the behavior and the XIB file defines the layout.
The code may have outlets into the XIB file to reference the layout elements.
The loader of the view could be different objects.
I'd like to be able to load the view by:
MyView *v = [MyView myView];
I've tried lots of different methods of setting the File's Owner and loading the XIB via NSBundle, but I keep getting key value coding problems.
Can anyone share the basic method to do this?
I have a repo of my current code here. As you can see, it generates a key value coding error.
Oh, I seem to have figured it out.
Keys are the following:
The XIB file UIView only needs to be a generic UIView. No need to set it to your subclass.
The File's Owner needs to be your subclass.
Outlets go to the File's Owner.
In your custom UIView, load the NIB as follows:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
UIView *v = [[nib instantiateWithOwner:self options:nil] lastObject];
v.frame = self.frame;
[self addSubview:v];
}
return self;
}
How exactly do you instantiate it? In my "container" class I have an outlet for my custom view as a property, but when I try to do self.customView = [[CustomView alloc] init];
the view does not come on screen, even though I know that it's correctly initialized because I can see the NSLog()s I put in my custom view's initWithFrame: method.
From within my main view controller, BEFORE instatiating the new class, the view is as I've defined it in the storyboard: <CustomView: 0x6a83bb0; frame = (38 153; 244 153); autoresize = RM+BM; layer = <CALayer: 0x6a83c60>>
However, inside the custom view's initWithFrame:, the frame is {{0, 0}, {0, 0}} and the view is <UIView: 0x68ca160; frame = (0 0; 0 0); autoresize = W+H; layer = <CALayer: 0x68d1800>>.
After the initialization has taken place, the main view's property for the custom view is barely (null), which baffles me.
I'm sure I'm missing something very simple here. Are you even supposed to define the custom view's within the main view in the storyboard at all?
Thanks!
There isn't a lot of questions about this and none of them were answered in a way that could solve my problem, still sorry if I'm asking something that was asked already.
My problem is that I'd like to use core plot to draw a graph inside a view. So I add the view on IB in my appviewcontroller.xib , but thenI can't find how to link that specific zone I just defined on IB to the file's owner.
A bit of precision: I managed to use core plot in another application, when it was the main view, but now that I want to add it to an existing application, so an existing view, it doesn't work...
And to add the new zone, I just went into IB, and dragged a view from the objects library onto my appviewcontroller.xib.
So how do I link that particular view to the code I wrote for it ?
Thanks for any help !
U have to add uiview created to self.view,then add graph hosting view to it...
Try this
- (void)loadView {
self.view =view1;
}
- (void)viewDidLoad {
[super viewDidLoad];
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
hostingView.backgroundColor=[UIColor whiteColor];
graph = [[CPXYGraph alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
hostingView.hostedGraph = graph;
[view1 addSubview:hostingView];
Create another view(view 2) in a main view(default view let it be view)in the storyboard(xib file) and make sure it (view 2) as outlet in the header file(.h).
Drag and Connect it to the newly created view in the storyboard(xib file) so after making necessary connections in the storyboard,storyboard should be like this:
View---view (default view connected to view) and
view 2---view (new view, view 2 connected to its view).
In the loadView method of implementation file,try out these code
-(void)loadView {
[super loadView];
newView = [[CPTGraphHostingView alloc] initWithFrame: [[UIScreen mainScreen]applicationFrame]];
[self.view2 addSubview:newView];
}
Hope you can help. I basically need to add an object to a UIView that is within a UIView.
In interface builder I have my View, and inside that I have another View with an image (map) inside (all done with Interface Builder). The second view I am using a class called "MainGameMapViewController" (reason being is that the first view is static/contains text/scores etc. and the view inside that is the game, and this whole view is draggable with the code in "MainGameMapViewController").
I need to add objects to "MainGameMapViewController" UIView.
I have added a class called "CharMain" and used this code inside the MainViewController:
-(void) viewWillAppear:(BOOL)animated {
CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
[self.view addSubview:charMain];
}
CharMain contains the code that draws a rect and adds an image, this works fine, BUT it needs to be added to the second view "MainGameMapViewController".
I have tried adding the code inside the MainGameMapViewController file, and it says "error: request for member 'view' in something not a structure or union", I have tried
[self addSubview:charMain];
This comes up with no errors, but the image doesnt appear, how do I make this appear within the second UIView? Thanks!
CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(40, 280, 0, 0)];
//this should be your subview instead of self.view try adding it to your subview's instance.
[MainGameMapViewControllerInstance.view addSubview:charMain];