KVO on my UIView CGRect property in iOS5 isn't working - model-view-controller

I have a ViewController and an UIView and would like to do KVO on an UIView property of type CGRect. For some reason it not working as it should.
My code looks like this:
#interface MyView : UIView
#property (nonatomic, assign) CGRect myRect;
#end
#implementation MyView
#synthesize myRect;
...
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
...
//Changing structures values
myRect.origin.x+=moveVector.x;
myRect.origin.y+=moveVector.y;
//assigning new structure
[self setMyRect:CGRectMake(20, 20, 50, 50)];
//Both doesn't call
}
I want to observe the CGRect with my viewController. Not sure how strict i should be with MVC here. Btw the CGRect in MyView is just a visible square, which i can move around, so i would say its not a model and should stay in MyView. Please correct em if iam wrong.
Here is my ViewController:
#class MyView;
#interface MyViewController : UIViewController
#property (nonatomic, retain) MyView *overlayView;
#end
#implementation MyViewController
#synthesize overlayView;
- (void)viewDidLoad
{
[super viewDidLoad];
overlayView = [[CameraOverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[overlayView addObserver:self
forKeyPath:#"myRect"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];
/* [self addObserver:self
forKeyPath:#"overlayView.myRect"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:NULL];*/
self.view = overlayView;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void*)context {
if( [keyPath isEqualToString:#"myRect"] ) {
NSLog(#"observeValueForKeyPath");
CGRect rect = [[change valueForKey:NSKeyValueChangeNewKey] CGRectValue];
}
}
observeValueForKeyPath is never called, regardless of how i change myRect in MyView.
setMyRect: crashes during runtime and i dont really know why. I though synthesize would take care of setters and getters as well as the Keyvalues and changes.
Iam also not sure which way of addObserver: is the better way, as i commented out my second attempt to register an Observer.
What iam doing wrong? Isn't it possible to Observe structures? Or only with self written setters and getter?
Can it be that UIView is not KVC-Compliant, if so, why does this work?
Callback by KVO on a UIView frame property
Thanks for any help :-)

My solution is, Iam using a delegate.
The Controller is my delegate for view, i think its the way it should be,
otherwise UIView would be KVC-Complaint.

Related

Mac OS X Cocoa multiview application navigation

I've already spent 2 full days trying to figure out how to use NSViewControllers in order to create a multiview application.
Here is what I do.
I have 2 View Controllers and the MainMenu.xib's Window.
I also have an AppController that is the delegate for both View Controllers.
When I launch the app, I'm first greeted with the MainMenu.xib's Window's view which holds a button. On clicking this button, an IBAction is sent to the appController and asks for the SecondViewController to display it's nib. So far, everything's fine and the nib file is displayed correctly.
On the secondViewController, there's another button that sends another IBAction to the appController and asks for the FirstViewController to be displayed but nothing happens,
no crash, no warning... Any help would be much appreciated...
Thanks in advance for your patience...
Here is the code for the AppController.h :
#import <Foundation/Foundation.h>
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface AppController : NSObject
#property (strong) IBOutlet NSWindow *mainWindow;
#property (strong) IBOutlet SecondViewController *secondViewController;
#property (strong) IBOutlet FirstViewController *firstViewController;
- (IBAction)secondButtonfromsecondViewControllerClicked:(id)sender;
- (IBAction)buttonClicked:(id)sender;
#end
and here is the code for the AppController.m :
#import "AppController.h"
#implementation AppController
#synthesize mainWindow = mainwindow;
#synthesize secondViewController;
#synthesize firstViewController;
- (IBAction)buttonClicked:(id)sender {
NSLog(#"button from second View Controller clicked");
self.secondViewController = [[SecondViewController
alloc]initWithNibName:#"SecondViewController" bundle:nil];
self.mainWindow.contentView = self.secondViewController.view;
[self.secondViewController.view setAutoresizingMask:NSViewWidthSizable |
NSViewHeightSizable];
}
- (IBAction)secondButtonfromsecondViewControllerClicked:(id)sender {
NSLog(#"button from first ViewController clicked");
self.firstViewController = [[FirstViewController
alloc]initWithNibName:#"FirstViewController" bundle:nil];
self.mainWindow.contentView = [self.firstViewController view];
}
#end
Well, anyone can help me, I just need a single view application that displays a first ViewController with a button on the first viewController that takes me to a second view controller with a second button that takes me back to my first viewcontroller... I've already spent more than a week on that... in vain... PS : I don't want any button on the mainMenu.xib window nor tabs.
here is the solution to my question then.
Here's the code for the AppDelegate.h:
// AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
//We need to declare the AppDelegate class as being the delegate for both
//FirstViewController and SecondViewController
#interface AppDelegate : NSObject <NSApplicationDelegate,
FirstViewControllerDelegate, SecondViewControllerDelegate>
#property (strong, nonatomic) NSWindow *window;
#property (strong) FirstViewController *firstViewController;
#property (strong) SecondViewController *secondViewController;
-(void) goToSecondView;
-(void) goToFirstView;
#end
Now, here's the AppDelegate.m:
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize firstViewController;
#synthesize secondViewController;
-(void) awakeFromNib {
[self goToFirstView];
self.firstViewController.delegate = self;
}
-(void) goToSecondView {
if (self.secondViewController ==nil) {
self.secondViewController =[[SecondViewController alloc]
initWithNibName:#"SecondViewController" bundle:nil];
}
self.window.contentView = [self.secondViewController view];
}
-(void) goToFirstView {
if (self.firstViewController ==nil) {
self.firstViewController =[[FirstViewController alloc]
initWithNibName:#"FirstViewController" bundle:nil];
}
self.window.contentView = [self.firstViewController view];
}
#end
Next we need to set delegates in the FirstViewController and the SecondViewController
// FirstViewController.h
#import <Cocoa/Cocoa.h>
#import "SecondViewController.h"
//We declare the delegation protocole:
#protocol FirstViewControllerDelegate <NSObject>
-(void)goToSecondView;
#end
#interface FirstViewController : NSViewController
- (IBAction)firstViewControllerButtonClicked:(id)sender;
#property (nonatomic, strong) id <FirstViewControllerDelegate> delegate;
#end
And here is the FirstViewController.m:
// FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.delegate = [NSApp delegate];
}
return self;
}
- (IBAction)firstViewControllerButtonClicked:(id)sender {
NSLog(#"button from first View Controller clicked");
if ([self.delegate respondsToSelector:#selector(goToSecondView)]) {
[self.delegate goToSecondView];
}
}
#end
Now, same thing for the SecondViewController:
// SecondViewController.h
#import <Cocoa/Cocoa.h>
#protocol SecondViewControllerDelegate <NSObject>
-(void)goToFirstView;
#end
#interface SecondViewController : NSViewController
#property (nonatomic, strong) id <SecondViewControllerDelegate> delegate;
- (IBAction)goToFirstViewControllerButtonClicked:(id)sender;
#end
And here's the SecondViewController.m:
// SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.delegate = [NSApp delegate];
}
return self;
}
- (IBAction)goToFirstViewControllerButtonClicked:(id)sender {
NSLog(#"button from Second View Controller clicked");
if ([self.delegate respondsToSelector:#selector(goToFirstView)]) {
[self.delegate goToFirstView];
}
}
#end
Well, I guess this code may be improved and if you have any suggestion, feel free to let me know. Hope it will help others.
THE PROBLEM: When the user presses a button in View2, you want View1 to appear. It's not.
STEP 1: You say that the button should be invoking an action on your AppController. Set a breakpoint (or add a diagnostic log) in that action, just to verify that it is, in fact, being invoked.
STEP 2: Think about what you want that action to do, precisely. My guess is that you want to hide View2 and show View1. Perhaps
[view2 setHidden: YES];
[view1 setHidden: NO];
(I'm not using your names here, of course.) Or you might animate the transitions, either cross-fading the views or moving them.
STEP 3: My guess is that STEP 2 will solve your problem. If it doesn't, use the debugger again to verify that view1 and view2 are not null. (If they're null, you probably have weak variables where you need them to be strong.)
STEP 4: In the unlikely event that you're still stuck, check the frames of view1 and view2. Perhaps view1 isn't where you think it is.
STEP 5: If you're still stuck, check the alphaValue of view1. Maybe you set it to be transparent, and it's being drawn transparently in the right place.
STEP 6: I bet there is no step 6!
This isn't much of an answer at the moment, however I have some concerns about your code that I wanted to work through with you.
Are you sure you have connected the outlets and actions in Interface Builder. Please verify this.
You don't need mainWindow as there is already a window property that points to the main window (verify this in Interface Builder). Also this looks wrong:
#synthesize mainWindow = mainwindow;
^
W
So dump that and just use the existing window outlet provided by Xcode.
Don't re-create the view controllers if they already exist:
if (self.secondViewController == nil)
{
self.secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController"
bundle:nil];
}
self.window.contentView = self.secondViewController.view;

Load UIView defined in XIB into UIView

following a post presented here, I tried to load a view defined in a XIB file into a view, currently being displayed. Basically, what I want to do is to replace the middle view (see screenshot below) with a different view when the user clicks on the button.
This is the simple view I want to load into the middle view:
And this is the source code of the ViewController:
ViewController.h
#import
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *loadxibButton;
#property (strong, nonatomic) IBOutlet UIView *middleView;
- (IBAction)buttonClicked:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)buttonClicked:(id)sender {
NSLog(#"click...");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
#end
When I click on the button, nothing happens, i.e. the new view will not be displayed. Can anybody please help?
Thanks a lot
Christian
Phillip, thanks a lot!
Instead of
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
self.middleView= nibView;
}
I now use this code:
- (IBAction)buttonClicked:(id)sender {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"NewMiddleContent" owner:self options:nil];
UIView *nibView = [nibObjects objectAtIndex:0];
nibView.frame = CGRectMake(261, 0, 532, 748);
[[[self view] viewWithTag:2] removeFromSuperview];
[[self view] addSubview:nibView];
}
Is this want you meant, i.e. is this like "best practice"?
Bye
Christian
Your view controller has a view property which, presumably, has three subviews (left, middle, and right). Instead of just setting a middleView property in your controller, your strategy should be to update the primary view's subviews array (removing the one that's to be replaced and inserting the new one).

Cocoa : Load NSViewController with nib

I have tried many possibilities I found on this site and read some explanations
from apple developer page, but seems like i couldn't resolve my problem about
loading NSViewController with/form NIB.
Files on Xcode Project look a bit like this :
SecondViewController.h
SecondViewController.m
SecondViewController.xib
AppDelegate.h
AppDelegate.m
MainMenu.xib
The main problem is how could i create the SecondViewController programatically with
the initial nib on SecondViewController.xib
Custom class of FileOwner on MainMenu.xib is NSApplication
Custom class of FileOwner on SecondViewController.xib is SecondViewController
There are some panels and window in MainMenu.xb (about window and preference panel)
This application has no main window (using notification icon on status bar)
SecondViewController.h
#interface SecondViewController : NSViewController {
BOOL fullScreenMode;
NSImageView *fullScreenbg;
NSImageView *imageView1;
NSImageView *imageView2;
NSPanel *imageWindow;
}
#property (assign) IBOutlet NSImageView *fullScreenbg;
#property (assign) IBOutlet NSImageView *imageView1;
#property (assign) IBOutlet NSImageView *imageView2;
#property (assign) IBOutlet NSPanel *imageWindow;
SecondViewController.m
#implementation SecondViewController {
NSImageView *nv1;
NSImageView *nv2;
NSSize curImgSize;
}
#synthesize fullScreenbg;
#synthesize imageView1;
#synthesize imageView2;
#synthesize imageWindow;
......
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
fullScreenMode = YES;
}
return self;
}
AppDelegate.h
#interface AppDelegate : NSObject <NSApplicationDelegate,NSWindowDelegate> {
NSPanel *aboutWindow;
IBOutlet NSMenu *myStatusMenu;
IBOutlet NSMenuItem *toggleFullScreen;
}
AppDelegate.m
#implementation AppDelegate {
SecondViewController *controller;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
controller = [[SecondViewController alloc]
initWithNibName:#"SecondViewController"
bundle:nil];
//Not work (fullscreenbg, imageView1, imageView2,imageWindow = nil)
//controller = [[SecondViewController alloc] init]; ?? <-- didn't work either
}
Even if using initWithNibName or just init, all the IBOutlet properties seems to be nil
on debug.
i've tried other solustions like "NSBundle loadNibNamed" or using loadView but it didn't work (warning message : "NSObject my not respond to loadView").
The main purpose of the secondViewController is to display notification message including
graphics and web element.
I hope someone could give me a best suggestion. Thanks.
This is normal behavior. IBOutlets are not connected in the constructor.
You can override viewDidLoad, call super and then do any initialization.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//added this line :
if (nibBundleOrNil!=nil || ![nibBundleOrNil isEqualtoString:#""]) {
[NSBundle loadNibNamed:#"SecondViewController" owner:self];
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
fullScreenMode = YES;
}
return self;
}

iAd on multiple view controllers

I have used iAd's before but only for apps with a single view controller. But I cannot seem to figure out how to create a global reference to the ad in the AppDelegate and fetch it from there for my separate view controllers (That's what I've read I'm supposed to do).
I've been searching for a tutorial on the matter, but for some reason I can't find anything relevant.
Any hints? Point me in the right direction? :)
TIA!
/Markus
In applications there is a adddelegate.h and .m file. You add iad in delegate.m file and create reference in other view :
in Appdelegate.h add delegate :
#interface AppDelegate : UIResponder
ADBannerView *bannerView;
#property (nonatomic, retain) ADBannerView *bannerView;
in Appdelegate.m :
#synthesize bannerView;
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
bannerView = [[ADBannerView alloc]initWithFrame:CGRectZero];
bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierLandscape,nil];
bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
bannerView.delegate = self;
}
Now you create reference of Appdelegate in other class viewdidload :
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
UIView banner = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 480, 32)];
[banner addSubview:appdelegate.bannerView];
[self.view addSubview: banner];

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;

Resources