How to get object id from UIButton ?
I want to know which uibutton is pressed currently when i don't save the pointer to uibutton.
Not alot to your question so it's hard to understand what you're asking... But, I'll hazard a guess by making the assumption that you're asking for how to get access to a UIButton from within your code? If so, then I'd need to also assume you'd added this button via Interface Builder in which case you simply need to add an IBOutlet pre-processor directive for each button object in your view controller (or view's) interface as such:
#interface MyView : UIViewController {
UIButton *myButton;
}
#property (nonatomic, retain) IBOutlet UIButton *myButton;
// Button's action which you probably already figured out
- (IBAction)buttonPressed:(id)sender;
Then you'll need to make the connections in Interface Builder. Drag FROM the object that needs to know TO the object it needs to know about. So you'd control-drag from the File's Owner to the UIButton to connect the IBOutlet...and then control-drag from the UIButton back to the File's Owner to connect the IBAction.
Related
Unlearned programmer here. Last time I used a NSTextView I was able to do it by adding a class to my xib file with all my outlets declared as such:
#interface Fusebox : NSObject
{
IBOutlet id object1;
IBOutlet id object2;
....
}
Now I am trying to code my interface properly, using the application delegate because Xcode 5 does adding a separate class file differently, if at all. I attempted to have Xcode's IB create the property of a NSTextView. But it wont creates this:
#property (weak) IBOutlet NSScrollView *results;
instead of what I need, which I think is this:
#property (weak) IBOutlet NSTextView *results;
An NSScrollView is completely useless for my purposes of outputting formatted (mainly line breaks) strings. When i try to hand code it as a NSTextView, it wont let me connect the outlet, only bind it. I am grabbing on a text view object from the library in the interface builder, so what am i doing wrong?
I think you aren't actually hitting the NSTextView - by default, it's imbedded in an NSScrollView. Xcode probably thinks you're trying to connect the outlet to the scrollview.
Try expanding the left pane of Interface Builder and connect the outlet to the sidebar:
Right-click-dragging from that highlighted row above to the code gives the expected result:
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.
I just jumped into Objective C.
When I create a button and connect it to my code, I get the following line of code in my property section:
IBOutlet UIButton *btn;
I've learned that a property syntax is [class] *[variable name].
What is IBOutlet in this case?
No. IBOutlet is simply a macro that resolves to nothing.
Their purpose is to simply let Interface Builder know that your variables (in your case UIButton *btn) can be used to link UI elements to your code within Xcode.
That is not a property. That is just a variable declaration. The property version of that would be
#property (nonatomic, retain) IBOutlet UIButton *btn;
And then in your implementation file, you would place
#synthesize btn
just below the #implmentation line.
Have you ever used interface builder? IBOutlet is a macro that lets you refer to views in interface builder from your code. In your case, it lets you hook up the UIButton to interface builder so that you can use a reference to it from within your code. Other than being used to let interface builder you want to hook up the variable to a view, it's not used at all and actually resolves to nothing.
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.
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