I have my viewcontroller with view with 2 buttons. If i press button i want to get new window with search bar + data from my DB. How can I achive it without new VC ?
2 Answers
You basically want to bring another view into your current view controller on the press of a button.
Here will be my approach :
Make a separate class for the view (in your case the search bar and the data from the DB). Also a separate XIB for this would be handy.
Let the class be named 'PopOver'
In Popover.m :
+ (PopOverView*) createInstance {
PopOverView *xibView = [[[NSBundle mainBundle] loadNibNamed:@"PopOverView" owner:self options:nil] objectAtIndex:0];
[xibView setFrame:CGRectMake(CGRectMinXEdge + 10, CGRectMinYEdge + 10, 200, 200)];
// adjust frame according to your need
[xibView setBackgroundColor:[UIColor redColor]];
return xibView;
}
Next : In your view controller, On the tap of the button you would add the following code :
- (IBAction)buttonTapped:(id)sender {
UIView *myPopoverView = [PopOverView createInstance];
AppDelegate *appDel = [UIApplication sharedApplication].delegate;
[appDel.window addSubview:myPopoverView];
}
NOTE : I added the 'PopOver' view to the app delegate's window instead of the VC because in your case you don't want the user to interact with the view controller when you are showing the 'Popover' window. In such cases, its better to present views in the app's window.
ALSO : You can animate your view while you present it. For this, refer this link : addSubview animation
Lastly, you will need to implement your search and your DB functionality within the PopOver view class. (Don't forget to add delegates between your VC and Popover).
Hope this helps.
Thanks !

- 1
- 1

- 545
- 1
- 6
- 19
-
Thanks , it works for me. I have question. Is any easy way to add X button in your solution ? I want just close popover – MRFREFERFRE Feb 12 '16 at 13:19
-
Sure. Just add a button in your PopOver XIB file. Give it a X (cross) image. Set the delegate of the button tap from your Popover.m to your VC and then remove the popover subview from window. – Pulkit Sharma Feb 13 '16 at 08:16
-
If its not problem could you write me this code which should be in Popover.m and ViewControler.m ? I am such bad in delegates , meybe it helps me understand – MRFREFERFRE Feb 13 '16 at 12:38
-
-
window is created by default in app delgate when you create a project in xcode and whole app is running on that window.
If I am not wrong you want to create a view and add it on a window.
- create a File->New->File->iOS(UserInterface), select view name it "YourView"
- create another File->New->File->iOS(Source), select CocoaTouch class, name it
"YourView" subclass of "UIView".
YourView *viewObj = [[[NSBundle mainBundle] loadNibNamed:@"YourView" owner:self options:nil] objectAtIndex:0];
AppDelgate *appDelObj = [[UIApplication sharedApplication] delegate];
[appDelObj.window addSubview:viewObj];
your view added on window.

- 21
- 2