-3

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 Answers2

1

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 !

Community
  • 1
  • 1
Pulkit Sharma
  • 545
  • 1
  • 6
  • 19
1

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.