Swap xib files in a single window

walden systems, objective c, geolocation, ios, CLLocationManager, desiredAccuracy, distanceFilter, startUpdatingLocation, stopUpdatingLocation, delegate, iphone, picture, screen, swipe, iphone, thread, nsthread, uitableviewcell, uitableview, initwithFName, infocellcontroller, nsdate, nsdateformatter nsstring, uuid, time, sleep, uiimage, UIGraphicsBeginImageContext, drawinrect, cgrectmake, nsuserdefaults, standarduserdefaults, setobjectforkey, objectforkey, UIControlEventTouchUpInside, resize picture, merge picture, stringByReplacingOccurrencesOfString, app group, appdelegate
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.

Here is an example of swapping different view controllers in a window. We will need 2 methods; one to recognize the event and another to swap the view. We will also need to be able to access the objects for the view and the window. We will us a NSMutableArray to hold the .xib file names and it's index.

In the sampleAppDelegate.h, we declare the objects and methods

1    @property (weak) IBOutlet NSBox *_view_area;
2    @property NSMutableArray *_view_list;
3    @property NSUInteger _view_idx;
4 
5    -(IBAction)swapViewOnButtonclick:(id)sender;
6    -(void)displayViewController:(NSViewController *)new_viewController;

In the sampleAppDelegate.m, we put in the implementation of the methods.

 1    @property (weak) IBOutlet NSWindow *current_window;
 2    ...
 3    ...
 4    ...
 5    -(void)displayViewController:(NSViewController *)new_viewController
 6    {
 7       NSWindow *new_window = [_view_area _current_window];
 8       BOOL ended = [new_window makeFirstResponder:new_window];
 9       if(!ended)
10       {
11          return;
12       }
13       NSView *new_view = [new_viewController view];
14       [_view_area setContentView:new_view];
15    }
16 
17    -(IBAction)swapViewOnButtonclick:(id)sender
18    {
19       if(!_view_idx)
20       {
21          _view_idx++;
22       }
23       else
24       {
25          _view_idx = 0;
26       }
27       NSLog(@"Current view is: %ld.
", _view_idx);
28       NSViewController *updated_viewController = [_view_list objectAtIndex:_view_idx];
29       [self displayViewController:updated_viewController];
30    }
31
32
33    -(IBAction)swapViewOnButtonclick:(id)sender
34    {
35       if(!_view_idx)
36       {
37          _view_idx++;
38       }
39       else
40       {
41          _view_idx = 0;
42       }
43       NSLog(@"Current view is: %ld.
", _view_idx);
44       NSViewController *updated_viewController = [_view_list objectAtIndex:_view_idx];
45       [self displayViewController:updated_viewController];
46    }