add UIView as a subview in UITableViewController (or sliding up menu) - objective-c

I am trying to add a subview in my UITableViewController. What I want to is when a user click a Navigation Bar Item, a small window or a subview will show which contains two buttons. When users click any button, the subview will disappear. I even have no success to add a subview in my TableViewController. I added following codes in ViewDidLoad:
UIView* simpleView = [[UIView alloc] initWithFrame:CGRectMake(0,200,300,100)];
UIButton* add= [[UIButton alloc] initWithFrame:CGRectMake(0,240,300,100)];
add.titleLabel.text=#"here";
[simpleView addSubview:add];
[self.tableView addSubview:simpleView];
which did not work, the error is
"setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key viewReference.'"
I change to [self.view addSubview:simpleView]; ,which still did not work
Can some one tell me how to add a subview in a UITableViewController and provide some sample code?
Thanks

Your button frame rect is incorrect so your simpleView is fully transparent. Change
UIButton* add= [[UIButton alloc] initWithFrame:CGRectMake(0,240,300,100)];
to
UIButton* add= [[UIButton alloc] initWithFrame:CGRectMake(0, 40,300,100)];

Related

calling method of superview from subview when a button is pressed on subview

I have a viewcontroller , onto which i m drawing uiview ,on this uiview there is a button . I want when this button is pressed a method is called on parent view controller that will draw a tablview onto viewcontroller
calenderView = [[UIView alloc] initWithFrame:CGRectMake(405, 230,[UIImage imageNamed:#"calendar.png"].size.width, [UIImage imageNamed:#"calendar.png"].size.height)];
calenderView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"calendar.png"]];
[[self.view superview] methodname];
Hope this helps...

UIView Modal Popup with Embedded UITableView with UIToolbar, Toolbar moves with Table

This is driving me insane! (ios 5+, ARC)
Pretty simple concept here:
I have a UIView with an embedded UITableView, when clicking on a specific cell, I have iOS launch a modalview with a segue.
This modalview, is a UIView with an Embedded UITableView in it, filled with names from a data source. You can choose multiple items in this view (using cellaccessory: checkmark)
The Goal
Get some sort of "Done" button to show up
Ok, so after much running around, I am to understand that Modal windows do not, in fact, allow navigationController items. No toolbars, no Navigationbars by default.
Ok, so I'll create my own.
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger tbHeight = 50;
UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)];
tb.translucent = YES;
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,doneButton,nil];
[tb setItems:barButton];
[self.view addSubview:tb];
barButton = nil;
//....
}
Simple enough right?
Well, the toolbar does show up, for sure. BUT, it doesnt stick to the bottom like its supposed to. In fact, when you scroll the embedded tableview up and down, the UIToolbar goes with it, almost like its stuck on the tablevie somehow.
Ive been looking for hours for solutions, I've come across nothing. Wondering if anybody here has any ideas?
If you need more information, please by all means, enquire :)
It's strange that your toolbar is scrolling with the table if it's a UIViewController subclass, unless you've assigned a UITableView to self.view or something... But since it is, this is what I do when adding a fixed item to a table view:
-(void) scrollViewDidScroll:(UIScrollView *)scrollView {
tb.frame = CGRectMake(0, self.view.bounds.size.height-tb.bounds.size.height+scrollView.contentOffset.y, self.view.bounds.size.width, tb.bounds.size.height);
}

Multiple Views in Xcode 4.2

I'm having a lot of trouble finding a tutorial for implementing multiple views in Xcode 4.2 without storyboard, this is for a class so I can't use storyboard yet. I'm just trying to have a 2nd view with a UIPicker come up when a button is clicked in the main view, I just can't find one for this version of Xcode and it's different enough from the older versions to confuse me.
Any help appreciated if someone can give me a quick description of what I need to do this or a newer tutorial I'd appreciate it :)
I think you should read the UIView Programming Guide to get a good handle on how UIViews work exactly. I find nibs/storyboard are really great at confusing new iOS developers.
In essence, a UIViewController has 1 view which you set in the viewDidLoad or loadView method by using the [self setView:someUIView]. You add more stuff to the screen by adding UIViews as a subview of the viewcontroller's "Main" view. For example
-(void)loadView {
// Create a view for you view controller
UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self setView:mainView];
// Now we have a view taking up the whole screen and we can add stuff to it
// Let's try a button, a UIButton is a subview of UIView
UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// We need to set a frame for any view we add so that we specify where it should
// be located and how big it should be!
[newButton setFrame:CGRectMake(x,y,width,height)];
// Now let's add it to our view controller's view
[self.view addSubview:newButton];
// You can do the same with any UIView subclasses you make!
MyView *myView = [[MyView alloc] init];
[myView setFrame:CGRectMake(x,y,width,height)];
[self.view addSubview:myView];
}
Now here we have our viewController who'se view is just a plain UIView which in turn has 2 subviews; newButton and myView. Since we created the MyView class, maybe it contains subviews as well! Let's take a look at what a UIView subclass could look like:
// Here is the init method for our UIView subclass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Let's add a button to our view
UIButton *newButton2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
// Of course, the frame is in reference to this view
[newButton2 setFrame:CGRectMake(x,y,width,height)];
// We add just using self NOT self.view because here, we are the view!
[self addSubview:newButton2];
}
return self;
}
So in this example we would have a view controller who'se view now contains 2 button! But the view structure is a tree:
mainView
/ \
newButton myView
\
newButton2
Let me know if you have any other questions!
Matt

Adding a subview without a XIB

This may seem painfully simple, but I'm just starting out so bear with me...
I have a main view with a toolbar. When the user presses a button in the toolbar, I would like a small prompt to appear at the top of the screen. I added a UIView within the existing UIView, and set it to be hidden. I connected that to an ivar called 'searchView' in my view controller, and tried the following:
searchView.hidden = NO;
When that didn't work I tried adding...
[self.view addSubview:self.searchView];
...which didn't work either.
Do I need to set the index somehow to bring it above the main view?
Is this approach just altogether wrong?
If you created the UIView from the interface builder, forget to use addSubview because interface builder it's doing this automatically.
To do that in graphical mode (IB), follow this steps:
Define an IBOutlet in your file "h" should be something like:
IBOutlet UIView * myHiddenView;
Then in IB, connect the IBOutlet that you have defined in the h file to the UIView that you have created graphically in the IB and give the hidden property to this UIView (checkbox).
Then in your button press method do this:
myHiddenView.hidden = NO;
If you prefer do it programmatically to copy this example code:
define in the interface of the .h file
UIView *myHiddenView;
after the interface in the .h file:
-(void)displayView:(id)sender;
then in the .m file:
-(void)viewDidLoad{
myHiddenView = [[UIView alloc] initWithFrame: CGRectMake (x, y, width, height)];
myHiddenView.hidden = YES;
[self.view addSubview: myHiddenView]
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"button" forState:(UIControlState)normal];
[button addTarget: self action: #selector(displayView:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)displayView:(id)sender{
myHiddenView.hidden = NO;
}

UIView transition and animation

I understand modal views cover the entire screen. But I really want a view that covers only half the screen just like the keyboard. So, please tell me why this doesn't work
MyController *controller = [[MyController alloc] initWithNibName:#"MyView" bundle:nil];
CGRect frame = CGRectMake(0,44,768,264);
[controller view].frame = frame;
contoller.delegate = self;
[[self view] addSubView:[controller view]];
[controller release];
I am trying to add a sub view to my current view and make it appear where the keyboard appears.
It throws a BAD ACCESS exception
In my code (above), I was using a custom UIViewController with it's own view [set to UIView on IB]. I couldn't get it to work by setting frame for the view controller's view.
So I added a custom UIView without a Nib file with all the controls (buttons, textfields) added on initWithFrame.
MyCustomView = [[MyCustomView] alloc] initWithFrame:frame delegate:self];
[self.view addSubView:MyCustomView];
Thanks for your comment, Jacob.