Navigation between NSVIewController - macos

I am very new to MAC OSX app development.
In my application I have three NSViewControllers, which are PracticeController, NoteController and QuestionController. I have to navigate to NoteViewController from PracticeController and QuestionController and comeback to the viewController from which NoteController has navigated.
For example: when we navigate to NoteController from PracticeController, then when we tap on back button from NoteController I have to come to PracticeController, and when we navigate to NoteController from QuestionController, then when we tap on back button from NoteController I have to come to QuestionController.
Please help me how to do this? I am badly searching for it. Thanks.

well, after a long time search, I found a open source library which ports the UIKit to MacOSX.
https://github.com/BigZaphod/Chameleon.git
But it's too complicated for me, so I wrote my own Navigation controller.
NSNavigationController.h
#import <Cocoa/Cocoa.h>
#class BaseViewController;
#interface NSNavigationController : NSResponder
#property (nonatomic, strong) BaseViewController *rootViewController;
- (id)initWithRootViewController:(BaseViewController *)rootViewController;
- (NSView*)view;
- (void)pushViewController:(BaseViewController *)viewController animated:(BOOL)animated;
- (BaseViewController *)popViewControllerAnimated:(BOOL)animated;
#end
NSNavigationController.m
#import "NSNavigationController.h"
#import "AppDelegate.h"
#import "BaseViewController.h"
#interface NSNavigationController ()
#property (nonatomic, strong) NSMutableArray *viewControllerStack;
#end
#implementation NSNavigationController
- (id)initWithRootViewController:(BaseViewController *)rootViewController
{
self = [super init];
if (self) {
self.rootViewController = rootViewController;
self.rootViewController.navigationController = self;
self.viewControllerStack = [[NSMutableArray alloc] initWithObjects:self.rootViewController, nil];
}
return self;
}
- (NSView*)view
{
BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
return topViewController.view;
}
- (void)pushViewController:(BaseViewController *)viewController animated:(BOOL)animated
{
if (viewController != nil) {
[self removeTopView];
[self.viewControllerStack addObject:viewController];
viewController.navigationController = self;
[self addTopView];
}
}
- (BaseViewController *)popViewControllerAnimated:(BOOL)animated
{
BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
[self removeTopView];
[self.viewControllerStack removeLastObject];
[self addTopView];
return topViewController;
}
- (void)removeTopView
{
BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
[topViewController.view removeFromSuperview];
}
- (void)addTopView
{
BaseViewController *topViewController = [self.viewControllerStack objectAtIndex:[self.viewControllerStack count] - 1];
AppDelegate *delegate = (AppDelegate*)[NSApp delegate];
[delegate.window.contentView addSubview:topViewController.view];
}
#end
BaseViewController.h
#import <Cocoa/Cocoa.h>
#class NSNavigationController;
#interface BaseViewController : NSViewController
#property (nonatomic, weak) NSNavigationController *navigationController;
#end
BaseViewController.m
#import "BaseViewController.h"
#interface BaseViewController ()
#end
#implementation BaseViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
#end
It's the simplest NavigationController. I didn't implement the view animation. Hope it can help.

Related

showsUserLocation action attached to button not working

I am having problems with a map view in my app. I have created a button that when clicked should show users location on the map, but nothing happens (no error messages occur).
I believe the issue may lie in the way I've written the delegates. The code from the relevant .h and .m files is below:
mapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface mapViewController : UIViewController {
MKMapView *mapview;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getCurrentLocation:(id)sender;
#property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
#end
mapViewController.m
#import "mapViewController.h"
#interface mapViewController ()
#end
#implementation mapViewController {
CLLocationManager *locationManager;
}
#synthesize mapview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType = MKMapTypeStandard;
break;
case 1:
mapview.mapType = MKMapTypeSatellite;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(IBAction)getCurrentLocation:(id)sender {
mapview.showsUserLocation = YES;
}
#end
Any help would be greatly appreciated, thanks
You have to implement the MapKit delegates. (Make sure you add the delegate signature in .h of your view controller)
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = userLocation.coordinate.latitude;
location.longitude = userLocation.coordinate.longitude;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
}
Extra: To handle App is on foreground or not:
We add 2 event observers to observe the App is entering background / returning active:
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appToBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)appToBackground{
[mapview setShowsUserLocation:NO];
}
- (void)appReturnsActive{
[mapview setShowsUserLocation:YES];
}

IKScannerDeviceView is only working once

I have an app with a main window and a secondary window with an NSScannerDeviceView in it.
The idea is to scan an image to memory and place it into an NSImageView in the main window.
So far so good. When the scanner window is opened I can scan an image and it is placed correctly in to the NSImageView.
The problem is this, I can scan again and again but if I close the scan window and re-open it the NSScannerDeviceView no longer works. I'm sure the solution is relatively simple but as I'm still learning obj-c / cocoa it's got me stumped.
The code I'm using is as follows: ScanWindowController.h
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <ImageCaptureCore/ImageCaptureCore.h>
#interface ScanWindowController : NSWindowController <IKScannerDeviceViewDelegate, ICScannerDeviceDelegate, ICDeviceBrowserDelegate> {
ICDeviceBrowser *mDeviceBrowser;
}
#property (strong) IBOutlet IKScannerDeviceView *ScannerView;
#end
and: ScannerWindowController.m
#import "ScanWindowController.h"
#import "AppDelegate.h"
#interface ScanWindowController ()
#end
#implementation ScanWindowController
#synthesize ScannerView;
- (id)init
{
if ( ! (self = [super initWithWindowNibName: #"ScanWindowController"]) ) {
return nil;
}
mDeviceBrowser = [[ICDeviceBrowser alloc] init];
mDeviceBrowser.delegate = self;
mDeviceBrowser.browsedDeviceTypeMask = ICDeviceLocationTypeMaskLocal|ICDeviceLocationTypeMaskRemote|ICDeviceTypeMaskScanner;
[mDeviceBrowser start];
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
}
- (void)windowWillClose:(NSNotification *)notification {
[mDeviceBrowser stop];
}
- (void)scannerDeviceView:(IKScannerDeviceView *)scannerDeviceView didScanToURL:(NSURL *)url fileData:(NSData *)data error:(NSError *)error
{
if(!error) {
[(AppDelegate*)[[NSApplication sharedApplication] delegate] recieveScannedData:data];
}
}
- (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
{
[scanner requestOpenSession];
}
- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing
{
if ( (addedDevice.type & ICDeviceTypeMaskScanner) == ICDeviceTypeScanner )
{
[ScannerView setScannerDevice:(ICScannerDevice*)addedDevice];
}
}
-(void)deviceBrowser:(ICDeviceBrowser *)browser didRemoveDevice:(ICDevice *)device moreGoing:(BOOL)moreGoing
{
[device requestCloseSession];
}
-(void)didRemoveDevice:(ICDevice*)removedDevice
{
[removedDevice requestCloseSession];
}
#end
I have solved this problem (after talking to Apple), and the result is this:
The NSScannerDeviceView is activated when the window is initialised, however as this doesn't get called again and the NSScannerDeviceView is closed when the window is closed, it never gets reactivated.
The solution was to create a new method (scannerReopen) and call it each time the window is opened as follows:
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import <ImageCaptureCore/ImageCaptureCore.h>
#interface ScanWindowController : NSWindowController <IKScannerDeviceViewDelegate, ICScannerDeviceDelegate, ICDeviceBrowserDelegate> {
ICDeviceBrowser *mDeviceBrowser;
}
#property (strong) IBOutlet IKScannerDeviceView *ScannerView;
-(void)scannerReopen;
#end
and:
#import "ScanWindowController.h"
#import "AppDelegate.h"
#interface ScanWindowController ()
#end
#implementation ScanWindowController
#synthesize ScannerView;
- (id)init
{
if ( ! (self = [super initWithWindowNibName: #"ScanWindowController"]) ) {
return nil;
}
if(!mDeviceBrowser) {
mDeviceBrowser = [[ICDeviceBrowser alloc] init];
[mDeviceBrowser setDelegate:self];
mDeviceBrowser.browsedDeviceTypeMask = ICDeviceLocationTypeMaskLocal|ICDeviceLocationTypeMaskRemote|ICDeviceTypeMaskScanner;
[mDeviceBrowser start];
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
}
- (void)windowWillClose:(NSNotification *)notification {
[mDeviceBrowser stop];
}
- (void)scannerDeviceView:(IKScannerDeviceView *)scannerDeviceView didScanToURL:(NSURL *)url fileData:(NSData *)data error:(NSError *)error
{
//process the scanned data...
if(!error) {
[(AppDelegate*)[[NSApplication sharedApplication] delegate] receiveScannedData:data];
}
}
- (void)scannerDeviceDidBecomeAvailable:(ICScannerDevice*)scanner;
{
[scanner requestOpenSession];
}
- (void)deviceBrowser:(ICDeviceBrowser*)browser didAddDevice:(ICDevice*)addedDevice moreComing:(BOOL)moreComing
{
if ( (addedDevice.type & ICDeviceTypeMaskScanner) == ICDeviceTypeScanner )
{
[ScannerView setScannerDevice:(ICScannerDevice*)addedDevice];
}
}
-(void)deviceBrowser:(ICDeviceBrowser *)browser didRemoveDevice:(ICDevice *)device moreGoing:(BOOL)moreGoing
{
[device requestCloseSession];
}
-(void)didRemoveDevice:(ICDevice*)removedDevice
{
[removedDevice requestCloseSession];
}
-(void)scannerReopen
{
[mDeviceBrowser start];
}
#end

Save UIImageView When Unload A View - Xcode 4.5.1

Creating an app on Xcode 4.5.1 and wondering how do i save the current image in the specified UIImageView when i navigate to a different view. Then when i return to the View it loads up the saved image into the UIImageView?
Thank a lot, appreciate it :D
The code I've posted below involves passing a uiimage to a second view controller. It also includes a button on each controller to move back and forth between the two. The saved image will stay in the uiimageview when you switch to and from the views.
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UIImageView *yourphotoview;
UIImage *yourimage;
}
#property (retain, nonatomic) UIImage *yourimage;
- (IBAction)back;
#end
SecondView.m
#import "ViewController.h"
#import "SecondView.h"
#interface SecondView ()
#end
#implementation SecondView
#synthesize yourimage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
yourphotoview.image = image;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)dealloc {
[image release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
#interface ViewController : UIViewController
{
IBOutlet UISlider *compression;
IBOutlet UISwitch *smurferderp;
SecondView *SecondViewdata;
}
#property (retain, nonatomic) SecondView *SecondViewdata;
#property (retain, nonatomic) IBOutlet UIImageView *theimage;
- (IBAction)switchview:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize SecondViewdata, theimage;
- (IBAction)switchview:(id)sender {
SecondView *secondview = [self.storyboard instantiateViewControllerWithIdentifier:#"rr"];
self.SecondViewdata = secondview;
SecondViewdata.yourimage = theimage.image;
[self presentViewController: secondview animated:YES completion:NULL];
}
- (void)dealloc {
[theimage release];
[super dealloc];
}
#end

Why doesn't my NSView delegate method gets called?

I'm working on my first Mac app and I'm having some trouble with getting my NSView and it's delegate methods to work. I have a NSViewController that should response to mouse events, i.e mouseDown. This didn't work, instead I created a custom NSView subclass that looks like this:
// Delegate method
#protocol CanvasViewDelegate
- (void)mouseDown:(NSEvent *)event;
#end
#interface CanvasView : NSView
{
id delegate;
}
#property (nonatomic, strong) id delegate;
#end
#implementation CanvasView
#synthesize delegate;
- (void)mouseDown:(NSEvent *)event
{
[delegate mouseDown:event];
}
The NSViewController relevant parts that should act as the NSView's delegate looks like this:
#import "CanvasView.h"
#interface PaintViewController : NSViewController <CanvasViewDelegate>
{
CanvasView *canvasView;
}
#property (strong) IBOutlet CanvasView *canvasView;
#end
#synthesize canvasView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
canvasView = [[CanvasView alloc] init];
canvasView.delegate = self;
}
return self;
}
- (void)mouseDown:(NSEvent *)event
{
NSLog(#"Down");
}
Now, the method mouseDown doesn't get called, what am I doing wrong?
If you want an NSView subclass to accept events, you will have to implement:
- (BOOL)acceptsFirstResponder {
return YES;
}
as documented here.
Creating the view programmatically instead did work. Like this:
NSRect canvasRect = self.view.frame;
canvasView = [[CanvasView alloc] initWithFrame:canvasRect];
[self.view addSubview:canvasView];
canvasView.delegate = self;

Expected expression error

What does this Expected expression error mean on this line of code:
[super setNilValueForKey:key];
Code:
#import "Person.h"
#implementation Person
- (id) init
{
self = [super init];
expectedRaise = 5.0;
personName = #"New Person";
return self;
}
- (void)dealloc
{
[personName release];
[super dealloc];
}
- (void)setNilValueForKey:(NSString *)key
{
if ([key isEqual:#"expectedRaise"])
{
[self setExpectedRaise:0.0];
}
else
{
[super setNilValueForKey:key];
}
}
#synthesize personName;
#synthesize expectedRaise;
#end
.h:
#import <Foundation/Foundation.h>
#interface Person : NSObject
{
NSString *personName;
float expectedRaise;
}
#property (readwrite, copy) NSString *personName;
#property (readwrite) float expectedRaise;
#end
If you really got
[super setNilValueForKey:<#key#>];
in your code, the solution should be as simple as
[super setNilValueForKey:key];
isEqual is the wrong method since I wanted to compare to a string value isEqualToString is correct.
- (void)setNilValueForKey:(NSString *)key
{
if ([key isEqualToString:#"expectedRaise"])
{
[self setExpectedRaise:0.0];
}
else
{
[super setNilValueForKey:key];
}
}

Resources