Switching of one view to another view in single customViews - cocoa

I am having of one CustomView in one xib and two different views in two different xib's.
I want to display those two view one after the other in one single CustomeView.
I have an object of NSView which is connected to CustomView in .xib file
#property (retain) IBOutlet NSView *mySubview;
#property (retain) NSViewController *viewController;
Method to open one View is:
-(IBAction)selectBookTicket:(id)sender
{
//setting status label to nil
_viewController=[[NSViewController alloc] initWithNibName:#"BookTicket" bundle:nil];
//loading bookTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
Method to open another view in same CustomView is:
-(IBAction)selectTicketCancellation:(id)sender
{
_viewController=[[NSViewController alloc] initWithNibName:#"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
When I am opening any view for first time its displaying properly in CustomView, but when I am trying to open second view or same view for second time then its get overlapping on previous opened view.
I tried
[_mySubview removeFromSuperview]
It's removing 'mySubview' completely, I mean what ever the view is currently loaded it is get removing but it's not allowing to display any views after that '[_mySubview removeFromSuperview]' get executed.

You must remove only the view that you have added from the view controller. Try the following code instead.
-(IBAction)selectTicketCancellation:(id)sender
{
[[_viewController view] removeFromSuperView];
_viewController=[[NSViewController alloc] initWithNibName:#"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
Executing [_mySubview removeFromSuperview] will remove your host view (i.e; the one that is displaying views from other view controllers) from view hierarchy and this explains the "not allowing to display any other subviews part".

Related

Xcode close view controller

I have two viewControllers parent and child, from parent I'm opening child viewController like this:
ClildVC *modal = [[ClildVC alloc] initWithNibName:nil bundle:nil];
modal.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:modal animated:YES];
and when I return from child View to parent, I use this:
[self dismissModalViewControllerAnimated:YES];
I want, when returning to parent viewController it be refreshed (reloaded), like I open it first time.
in your parentViewController in .h and .m add method
- (void)refreshData
{
//refresh your data
}
in your childViewController type this
- (IBAction)backToParent
{
YourParentController *parent = (YourParentController *)self.parentViewController;
[parent refreshData];
[self dismissModalViewControllerAnimated:YES];
}
dismissModalViewControllerAnimated: is deprecated as of iOS6
You should use dismissViewControllerAnimated:completion: which was introduced in iOS5 in the child view controller after calling a data update on its parent view controller
You are initiating with no nib file and no bundle identifier.
So its looking for a non existent nib in a bundle that isn't there
either design the nib in IB (xcode 4) or storyboard (4.2 +) or programatically by using the designated initialiser for the modal view controller.

Xcode best way to re-use area/view for multiple forms

I'm new to Xcode and starting a universal app. In the iPad version I want to re-use the main section for different search forms.
I want the user to see a completely different form in the section to the right (see image) for each button on the left menu.
I'm working programmatically.
What's the best way to change the content in this situation? (keeping in mind performance and coding complexity)
EDIT: This is a standard tabbed application, not a split view. Could I have a new view controller and .xib for each form and then change which one is displayed by embedding them in a UIView or UIWindow on this screen?
You've hit upon the right answer in your edit. From what I understand the buttons on the side are analagous to another tab bar controller, but nested within your main tab bar controller?
You might as well follow the same pattern, since it is a familiar one. Your search view controller should act as a container view controller, which would have an array of view controllers representing each form option.
As you switch between the options, add/remove the appropriate view controller views from your view hierarchy (using addSubview) and view controller hierarchy (using the code in "Adding and removing a child" in the link). The view controller hierarchy is important to ensure that viewDidAppear and so forth is called on your child controllers.
As an illustration, I've created a simple demo project where the main view controller has a set of buttons, each linked to the same action, and an array of contained view controllers. The contained view controllers will be held inside a subview, called container in this example. This would be the size of the right hand area in your screenshot above.
The child controllers are set up as follows in viewDidLoad of the container view controller:
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttons;
#property (weak, nonatomic) IBOutlet UIView *container;
#property (nonatomic,strong) NSArray *viewControllers;
#property (nonatomic, strong) UIViewController *currentChild;
#end
#implementation JRTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (UIButton *button in self.buttons)
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
NSMutableArray *children = [NSMutableArray new];
for (int i = 0; i < 4; i++)
{
ContainedViewController *child = [ContainedViewController new];
child.name = [NSString stringWithFormat:#"Form %d",i + 1];
[children addObject:child];
}
self.viewControllers = [children copy];
[self buttonTapped:self.buttons[0]];
}
Here I've used four instances of the same view controller class - all it has is a label which indicates which form you are selecting. Really you'd have different classes for each one. I've also "selected" the initial view controller by sending the action method for the first button. The action method does this:
- (IBAction)buttonTapped:(UIButton *)sender
{
NSUInteger index = [self.buttons indexOfObject:sender];
if (index != NSNotFound)
self.currentChild = self.viewControllers[index];
}
Which selects the appropriate VC from the view controller array. The setter method does this:
-(void)setCurrentChild:(UIViewController *)currentChild
{
if (currentChild == _currentChild) return;
// Remove the old child
[_currentChild willMoveToParentViewController:nil];
[_currentChild.view removeFromSuperview];
[_currentChild removeFromParentViewController];
[_currentChild didMoveToParentViewController:nil];
// Add the new one
[currentChild willMoveToParentViewController:self];
[self.container addSubview:currentChild.view];
NSDictionary *views = #{#"view":currentChild.view};
[self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[view]|" options:0 metrics:nil views:views]];
[self.container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:views]];
[self addChildViewController:currentChild];
[currentChild didMoveToParentViewController:self];
_currentChild = currentChild;
}
Which sets up the view controller hierarchy, adds in the view and uses constraints to make it fill the container.
In this example I've hardcoded in four buttons and four child view controllers as I was demonstrating the addition and switching of child view controllers. In reality you'd make it more like a tab bar controller where you assign an array of view controllers and the container would make its own array of buttons.

How do I put the name of the IBAction pressed on my main view controller in a NSString on my modal view?

I have about ten IBActions on my main view that go to the same modal view and I need to know how to see what IBActon was pressed to get there and put that in a NSString on the modal view.
Here's the code that sends you to the modal view. The only difference in the IBActions is the name. Like playMovie: and playMovie2: and so on.
-(IBAction)playMovie:(id)sender{
VideoSubViewController *subView = [[[VideoSubViewController alloc] init] autorelease];
[subView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:subView animated:YES];}
You could create an init* on the modal view that takes that information. The usage would be:
VideoSubViewController *subView =
[[[VideoSubViewController alloc] initForAction:#"playMovie"] autorelease];
You then store that information in the view instance in order to use it.

Cocoa: Adding a subview to a view from a different class and nib

If i have two nibs with several views, is there a way for me to use the addSubview: method between them? What I would like to do is take a view from one of the nibs and tell it to add a subview that would be a view in the other nib file.
The reason I have them in separate nibs is because the subview from the second nib will be added several times, using the same template but different parameters.
Yes, you can add a view in one nib as a subview to the view in another nib.
You need to create a NSViewController object which will own the child nib. So that as soon as you initialize the view controller the nib associated with it is loaded. Now you can use the view property of the controller and add it as a subview to any other view.
The code below will help you understand better:
YourViewController.m
-(id)init
{
self = [super init];
if(nil != self)
{
[NSBundle loadNibNamed:#"myNibName" owner:self];
}
return self;
}
YourOtherClass.m
-(void)addYourViewControllerViewAsSubview
{
YourViewController *yvc = [[YourViewController alloc] init];
[yourOtherViewOutlet addSubview:yvc.view];
}

Push a view from a custom table view cell Xcode 4

I was wondering if there was a way to have the user create/delete cells in a Table View but when they click on it, every cell that they create loads the same view. I have the first part created but I cannot seem to get it to load a view.
Simple create a new view and push on didSelectRowAtIndexPath Delegate method of UITableView.
For ex.
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
DetailViewController *vController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}

Resources