hi i'm studying iOS programming.
i have a wondering using interface builder.
i make a tableViewController and also make .xib file
now i can see the UITableView in interface builder.
i have to add a view, called myView, that contains buttons, labels and so on.
i want to myView be set the top of tableView's area, like tableview's header.
so i make a myView, and add buttons, labels, etc.
and i drag that view into UITableView. ok here's fine.
i can see myView is set the top of UITableView in interface builder.
but i run the program, myView doesn't appear.
of course wire up using IBOutlet, and declare property and synthesize.
but i use NSLog like this
if(self.myView == nil)
NSLog(#"omg it's nil!!!");
i can't understand that NSLog is printed my prompt area.
because i make that view in interface builder!
i can see tableView, of course can see the cells.
why myView doesn't appear??
how can i fix it??
i want to know how can i fix it using interface builder.
please help me
Not sure if this is possible using interface builder, I usually create a view manually and add it to the tableview header in the viewWillAppear method like so:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 60)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10,self.tableView.bounds.size.width, 50)];
label.text = [person getFullName];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:25];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);
[headerView addSubview:label];
self.tableView.tableHeaderView = headerView;
A simpler method would be to create a view in a separate nib file and then load it into the table header when you load the tableview like this:
UIViewController *aViewController = [[UIViewController alloc] initWithNibName:#"MYXIBFILEHERE" bundle:nil];
self.tableView.tableHeaderView = aViewController.view;
My experience with XCode 4.3:
you have to first place UITableView and UIView on the same super UIView. Then, when dragging UIView to the top of UITableView you'll notice XCode highlighting the option of inserting your view as footer.
Once done, you can detach UITableView from superview and delete it.
The only way I've been able to get this to work is as follows:
Add new nib file to project. Lay out the view in the nib file as you
want your table footer/header to look.
Set the class of the File's Owner in the nib to the class of your
UITableViewController. This is so we can connect the view in this
nib to an IBOutlet in the UITableViewController class.
Set a UIView IBOutlet in your UITableViewController to point to the
view in the new nib.
In your UITableViewController class, do something like this in
viewDidLoad:
(apparently the code won't format correctly if it's directly after a numbered list.)
[[NSBundle mainBundle] loadNibNamed:#"statsFooterView" owner:self options:nil];
self.tableView.tableFooterView = myFooterView;
Related
I have a UIViewController with a UITableView on top of it. I am trying to put a small UIView overlay on top of the table view. I added it both programatically (and used bringSubviewToFront), and then in Interface Builder and it does not appear either way.
Why is that? How can I add the UIView overlay on top of the table view?
EDIT:
I am not using a UITableViewController. It's a UIViewController with a UITableView. Here is the code I used in viewDidLoad:
UIView *joinMediame = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 44)];
joinMediame.backgroundColor = [UIColor orangeColor];
[self.view addSubview:joinMediame];
[self.view bringSubviewToFront:joinMediame];
If you are intending for your view to scroll with the table content, I would make it the tableHeaderView of the table. If you want the view to be static at the top, I would add it as a subview of the controller and then resize the table view frame to make room for the static view.
Note that if the controller is a UITableViewController, self.view is a UITableView, so if you do something like [self.view addSubview:staticView] this won't have the effect you expect.
It's a little bit to explain why I'd like to implement this design.
But the question I want to ask is : If I have a view and it's controller is ControllerA, and now I want to add a subview in my that view suppose View1. And that subview View1 contains a button, which I set the IBAction point to the ControllerA.
But I found that this View1 button cannot change some properties in original view's object like UIImageview.hidden.
Can't a button in subview alter things inside superview ? Or I need to set up other things to finish this task.
i guess you were try to add a button inside the UIView which is also a subview of View(UIViewController).
as you told whenever you try to access your Button property then you could not do same.
this is happening because whenevre we create anything through the XIb if we want access that UIControl or whatever then we have make Reference of that in Our Source Code.so you'll have hook up the UIButton with reference from the Xcode.
As i am doing in below Image.
I hope i got your Point. it'll be helpful to you
It's hard to understand exactly what you're saying but it sounds like you're trying to access a subview of a UIView instance outside of that UIView. If it is not a UIView subclass where you have an ivar/property reference to that subview, you can try giving it a tag and accessing it that way.
example:
UIView *topLevelView = [[UIView alloc] init];
UIView *viewA = [[UIView alloc] init];
UIView *otherView = [[UIView alloc] init];
otherView.tag = 5;
[topLevelView addSubview:viewA];
[topLevelView addSubview:otherView];
UIView *viewASubview = [[UIView alloc] init];
[viewA addSubview:viewASubview];
UIView *referenceToOtherView = [viewASubview.superview.superview viewWithTag:5];
UIView *anotherReferenceToOtherView = [topLevelView viewWithTag:5];
I've got an app with several UITableViews in it, and if one of them is empty, I'd like to display a bit of help text, floating in front of the UITableView:
"Looks like you haven't created anything for this list yet. Make your first entry by tapping the button above."
How do I present this view, floating in front of the UITableView?
What method of the UITableViewDelegate would be used to present this view?
What would the code look like to present the view?
What method of the UITableViewDelegate would be used to hide this view once the user adds content?
Here's a quick mockup:
declare in .h file
UIView *root;
in .m file
import QuartzCore/QuartzCore.h
- (void)viewDidLoad
{
[super viewDidLoad];
root = [[UIView alloc] initWithFrame:CGRectMake(60, 50, 220, 250)];
root.backgroundColor=[UIColor grayColor];
root.layer.cornerRadius=10;
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];
label.backgroundColor=[UIColor clearColor];
label.numberOfLines=3;
label.font=[UIFont fontWithName:#"Arial" size:14];
label.lineBreakMode=UILineBreakModeTailTruncation;
label.textColor=[UIColor whiteColor];
label.text=#"Make your first entry by tapping the button at the top of the screen";
[root addSubview:label];
[self.view addSubview:root];
}
inside your button Event method
[root removeFromSuperview];
in viewDidLoad/Appear,
create a view and add it as subview [self.view addSubview:infoView] . on click event of + button remove it from superview [infoView removeFromSuperView]
I would create/remove the view in numberOfRowsInSection (if it is 0 then show the view) or whatever it is called. As for the view itself, have a UILabel in a custom view. To add the rounded corners etc you can access the view's layer, which is a CALayer. Oh and make sure that you have an ivar for the view so you can remove it easily and check if it is visible. And make sure you always reload the table view when you show your view, as otherwise numberOfRowsInSection will not be called.
I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView that has an UIImageView and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = #"some new text". It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?
Should I be creating a new class that is a sub-class of UIViewController or just UIView?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
#implementation myView
#synthesize imageView, myLabel;
- (id) initWithFrame:(CGRect) frame
{
if (self = [super initWithFrame:frame]) {
// Setup your image view and add it to the view.
self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
self.imageView.image = ...
[self addSubview:self.imageView];
// Setup your label
self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
self.myLabel.text = #"whatever you like";
[self addSubview:self.myLabel];
}
return self;
}
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.
I'm looking for opinions on the best way to implement the following functionality. I have a UIViewController with a UITableView in Grouped Format. Each TableViewCell contains an NSString. When the User taps on a cell I'd like to in my didSelectRowForIndexPath method popup a UIView with a single textfield, that's prepopulated with the NSString in the given cell that was selected. The reason for displaying the UIVIew is I want it to be about 280 x 380 frame and still see some of the UITableView.
The goal being that it behaves like a ModalViewController except for the iPhone. Does anyone have any suggestions on how to implement this behavior or if there is a better implementation?
Create the UIView (with the UITextField inside) beforehand, and make it hidden:
// Of course, these instance variable names are made up, and should be changed
self.myModalView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 380)] autorelease];
[self.view addSubview:self.myModalView]
self.myTextField = [[[UITextField alloc] init] autorelease];
[self.myModalView addSubview:self.myTextField];
self.myModalView.hidden = YES;
Then, when the user selects a row, populate the text field and show the modal view:
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
// Replace stringAtIndexPath with however your data source accesses the string
NSString* myString = [self stringAtIndexPath:indexPath];
self.myTextField.text = myString;
self.myModalView.hidden = NO;
}
If you want to get fancy, you can do some CATransition stuff before showing the modal view.
I think you can use "addSubView" in UITableView. Add the ModalView to your UITableView directly. It will work.
Use some animation to implement this effect. When the UIView appears, it will lift the UITableView, like some keyboard behavior. So, you have to addSubView on the self.view and modify the UITableView's frame. And, the UITableView should be a child view of self.view, if self.view is the same as the UITableView, then you has no self.view for adding this UIView.