Can't access #Property from other class - objective-c

I'm having trouble accessing some variables in my program.
I have one class called MainMenu.
In the .h file I have declared 2 properties as follows:
MainMenu.h:
#property (nonatomic, retain) IBOutlet NSView *mainView;
#property (nonatomic, retain) IBOutlet NSWindow *theMainWindow;
In another class file, I want to be able to access these 2 variables, current I am using the following code in the other .h class file which does not work, I don't understand what I'm doing wrong:
AppDelegate.m:
MainMenu *theMainMenu = [[MainMenu alloc] init];
[theMainMenu switchViews:theMainMenu.theMainWindow:theMainMenu.mainView];
Here I create an object of the MainMenu class, and invoke a method called 'switchViews' in its definition, I then want to pass it the 2 variables which I'm having trouble accessing.
Any help greatly appreciated.
Thanks in advance everyone.
EDIT: 'switchViews' method shown below:
- (void)switchViews:(NSWindow*)mainWindow:(NSView*)newView {
NSView *dummyView;
[mainWindow setContentView:dummyView];
[mainWindow setContentSize:newView.frame.size];
[mainWindow setContentView:newView];
}

I think the problem is one of 2 different instances of your MainMenu class. If you hooked up your IBOutlets, you must have a blue cube in IB set to your MainMenu class, correct? However, when you alloc init one in your app delegate, that creates another instance of MainMenu that doesn't have those properties connected to anything. Instead, you should also have a blue cube in IB set to your app delegate, and have an IBOutlet in that class that you connect to the MainMenu blue cube in IB.

This might be silly, but did you remember to link your NSWindow and NSView in Interface Builder? Or are you at least manually instantiating them?
My best guess is (assuming there is a MainMenu.xib somewhere), that when you call init, it object is inited but not necessarily loaded. You should try to call your switchViews method in your controllers viewDidLoad or viewWillAppear method to be sure it is all loaded.

Related

View not updating when a message is sent to its view controller

I am having problems updating a view when a message from another class is sent to a ViewController.
Basically I have an application with a single window where different custom views will be swapped out for another. I have an AppController Class that manages this and works fine:
#interface AppController : NSObject
#property (weak) IBOutlet NSView *ourView;
#property (strong) NSViewController *ourViewController;
- (IBAction)changeView:(id)sender;
- (IBAction)start:(id)sender;
- (void)changeViewContoller:(NSInteger)tag;
#end
When a new view is swapped out for another, the ourViewController property will be updated to point to that view's controller class. Every view controller class will have a method all named the same thing, for example "action". This method is supposed to change something on a view.
So the "start" method in AppController class will then call the "action" method on the ourViewController property. To do this I used the objc_msgSend() method:
objc_msgSend(self.ourViewController, action);
Here's the View Controller class definition:
#interface CountdownViewController : NSViewController
#property (weak) IBOutlet NSTextField *label;
- (IBAction)changeLabel:(id)sender;
- (void)start;
#end
I placed an NSLog() in the "action" method for each ViewController, to see if it was working, and it does, however the "action" method is also supposed to change a label's string value, but it does not. If anyone knows why the view is not being updated, that would be extremely helpful. Thanks!
the view is held weak?
TRY making it strong if you need to retain that pointer in this class
btw: ..also why do you objc_msgsend.... use performSelector

Creating Delegate in UIScrollView Subclass

I have subclassed a UIScrollView to customize it a bit. I am trying to create a delegate that will notify several other classes that a user has done a certain thing in the UIScrollView. In my UIScrollView class I have the code below. The problem I am running into is I am getting the warning
Property 'delegate' 'retain (or strong)' attribute does not match the
property inherited from 'UIScrollView'
I see that this is because my Class in inheriting from UIScrollView, but my delegate is conforming to the NSObject. This is the first time I tried creating my own delegate. What can I do to fix this?
My Code:
#import <UIKit/UIKit.h>
#protocol ChangeSpaceDelegate <NSObject>
- (void)changeSpace:(int)spaceId;
#end
#interface CustomUIScrollView : UIScrollView {
id<ChangeSpaceDelegate> delegate;
}
#property (retain, nonatomic)id delegate;
#end
To answer your question specifically, you are redefining the property attribute on the delegate property you get from UIScrollView. It should, like all delegates, be weak (or, pre-iOS 5, unsafe_unretained).
However, you shouldn't do it this way. UIScrollView already has a delegate, and if you expect to put your own delegate object implementing your new delegate methods into it, the inner workings of UIScrollView aren't going to be happy. Define a new protocol and a new delegate property for it.
#property (weak, nonatomic) id<ChangeSpaceDelegate> changeSpaceDelegate;
You don't have to create the delegate object in custom scrollview class since you are subclassing it from UIScrollView. You can directly use it as self.delegate in your custom scrollview class.
As mentioned by #Steve Madsen, I often add own delegate properties for subclasses. Like UITableView has separate DataSource and Delegate properties, and being assigned with the same object. In a long run, this will definitely pay off by not forcing you to repeat what have been already implemented in super class, and keeping your subclass implementations more manageable

Accessing IB object/controll from a class method

Are the objects/controls that you created using IB accessible from a class method?
#Nekto:
#interface CopyController : UIViewController
{
UIActivityIndicatorView *myActivity;
}
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *myActivity;
+(void) activityIndicator:(BOOL)flag;
#end
This implementation in the .m will not be allowed, the error was "Instance variable'myActivety' accessed in class method".
+(void)activityIndicator:(BOOL)flag
{
if (flag)
[myActivity startAnimating];
else
[myActivity stopAnimating];
}
Yes, they are accessible.
You should add #property IBOutlet ib_object_class *ib_object_name;, open that object settings in IB and set reference outlet to File's Owner by selecting ib_object_name in drop down menu.
Full explanation can be found, for example, here : Creating and Connecting an Outlet
You may be able to connect the outlet to the first responder instead of the file's owner to achieve this, but I don't think you can access it from within a class method since your IBOutlet property is going to be an instance-level variable.
Found something similar for linking actions to multiple first responders here.

Obj C delegate methods not assigned to UIButton (iOS)?

I am trying to assign a delegate's method to a UIButton using addTarget:action:forControlEvents:. Everything compiles without warnings, the IBOutlet is connected to the button in Interface Bulder (XCode 4). If I moves the delegate's method to the controller, it works fine. (All code worked fine, but I refactored to use a delegate, it's my first try with delegates and protocols.)
(added) The protocol declaration, placed before #interface in the .h:
#protocol MMGLVDelegate<NSObject>
-(void)receiveQuitRequest:(id)sender;
#end
In the controller interface, these properties:
#property (nonatomic, assign) id<TheDelegateProtocol> delegate;
#property (nonatomic, retain) IBOutlet UIButton *quitBtn;
In the controller implementation:
-(void)setDelegate:(id<MMGLVDelegate>)delegate {
DLog(#"MMGLVSegmented setDelegate: Entered");
_delegate = delegate;
[self.quitBtn addTarget:self.delegate action:#selector(receiveQuitRequest:)
forControlEvents:UIControlEventTouchUpInside];
}
Any help appreciated. Changing target to any of self.delegate, _delegate, or delegate doesn't change app behavior.
What I'm hoping to do is not have to declare a class receiveQuitRequest: that then passes off to the delegate, I'd rather go straight to the delegate from the control.
I think you should write
[self.quitBtn addTarget:delegate action:#selector(receiveQuitRequest:)
forControlEvents:UIControlEventTouchUpInside];
I am not sure, but this may work in your case
I have a couple of minor suggestions:
Try disconnecting and re-connecting the delegate for the button in IB. I've noticed that sometimes this seems to reset it properly.
Clean and rebuild. Again, this helps to reset things that didn't get set properly.
If I've understood everything correctly, the UIButton does not include a receiveQuitRequest: selector, so there is nothing to be executed when the user touches the button.

When to alloc/init an ivar, and when not to

All,
In Apple's sample code "DateCell"
http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
the ivar "pickerView" is declared in MyTableViewController.h like this:
#interface MyTableViewController : UITableViewController
{
#private
UIDatePicker *pickerView;
UIBarButtonItem *doneButton; // this button appears only when the date picker is open
NSArray *dataArray;
NSDateFormatter *dateFormatter;
}
#property (nonatomic, retain) IBOutlet UIDatePicker *pickerView;
...
It is synthesized in the class file MyTableViewController.m like this:
#implementation MyTableViewController
#synthesize pickerView, doneButton, dataArray, dateFormatter;
...
When this app runs, I can insert NSLog(#"%#",pickerView) into ViewDidLoad and see that, sure enough, the ivar pickerView is real and has a value. Nowhere, though, does this class alloc/init pickerView. And that's the root of the question: how's it getting done if it's not being done explicitly?
Well, I naively copied this stuff to my code into my RootViewController.h and .m files figuring I could do the same, but pickerView stubbornly remains uninitialized (and my NSLog calls return "(nil)" as its value) no matter what I try short of explicitly alloc/initing it. Certainly RootViewController is being instantiated, or the RootView wouldn't be showing up, right? So shouldn't my pickerView be coming along for the ride just as it does for Apple?
So... do I have to manually alloc/init the pickerView instance variable? If so, where's Apple doing it? Or how are they doing it somehow otherwise?
I think I'm missing something very basic here, but I have no idea what it is. I can't see anything in Interface Builder or XCode that looks different between mine and theirs, but I've got tunnel vision at this point and can't see anything clearly anymore.
Thanks,
Bill
The IBOutlet modifier on this line is the key...
#property (nonatomic, retain) IBOutlet UIDatePicker *pickerView;
IBOutlet is a decorator that indicates that the object will be hooked up/connected/initialised when the corresponding xib (Interface Builder) file is loaded. The sample application you're looking up will contain a UITableViewController is a xib which has a connection to a UIPickerView.
You can either go the route of creating your own custom xib file and wire to an instance of UIPickerView or you can manually initialise the picker yourself.
Interface Builder (nib or xib) treats automatically IBOutlet ivar with connection of components.
IBOutlet is a special keyword that is
used only to tell Interface Builder to
treat an instance variable or property
as an outlet. It’s actually defined as
nothing so it has no effect at compile
time.
Your First iOS Application - The
View Controller Interface
Declaration, Making Connections
Interface Builder User Guide -
Defining Outlets and Actions in
Xcode