I am attempting to hide a NSButton that performs miniaturize when a different NSButton on the interface is clicked. My attempts thus far have been unsuccessful, this is what I have tried:
.h file:
#interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSWindow *window;
IBOutlet WebView *webView;
IBOutlet NSButton *doMinimize;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSButton *button;
#property (nonatomic, retain) IBOutlet WebView *webView;
.m file:
#implementation AppDelegate
#synthesize window;
#synthesize webView;
#synthesize doMinimize;
- (IBAction)toggleFullscreen:(id)sender
{
...
[doMinimize setEnabled:NO];
[doMinimize setTransparent:YES];
...
}
It appears that no matter in what action I try to disable and make the button transparent, it doesn't seem to respond to anything. Do I have to give the button it's own class to make this work? If so, how would I then be able to modify that button from an IBAction inside of a different class?
I apologize in advance if my question is silly, I'm relatively new to the world of Objective-C and am just now starting to get my feet wet.
Thanks in advance.
Have you tried -setHidden:?
[doMinimize setHidden:YES];
Related
I'm new to objective-c. When I'm reading some source code written by others, I encountered a problem.
I found that there is
IBOutlet NSPopover *popover;
as well as
#property NSPopover *popover;
PopoverViewController.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "TimerPopoverViewController.h"
#class TimerLogic;
#class TimerInfo;
#interface TimerPopoverDelegate : NSObject <NSPopoverDelegate> {
#private
IBOutlet NSPopover *popover;
IBOutlet NSWindow *detachWindow;
IBOutlet TimerPopoverViewController *viewController;
}
#property NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
#end
I think they are different variables. However, I can't figure out what do they do?
As far as I'm concerned, the IBOutlet is to show a popover.
But what does the #property does?
This is either very old code or written in a very old (and now discouraged) style. The IBOutlet here is declaring an instance variable (ivar). The #property is declaring a property that is backed by the instance variable. In modern ObjC you should implement it this way:
PopoverViewController.h
#import <Cocoa/Cocoa.h>
#class TimerInfo;
// Things declared here are public
#interface TimerPopoverDelegate : NSObject <NSPopoverDelegate>
// You could leave this here if it is required by other parts of the program,
// but other parts of the program really shouldn't require it. See below.
// #property (nonatomic, readonly, weak) NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
#end
PopoverViewController.m
// Generally avoid importing local headers into the .h unless you have to.
#import "TimerPopoverViewController.h"
// Things declared here are private. This is much better than the old #private.
#interface TimerPopoverDelegate ()
#property (nonatomic, readwrite, weak) IBOutlet NSPopover *popover;
#property (nonatomic, readwrite, weak) IBOutlet NSWindow *detachWindow;
#property (nonatomic, readwrite, weak) IBOutlet TimerPopoverViewController *viewController;
#end
(Currently popover is public, but you should avoid exposing an IBOutlet that way. Outside objects should not directly touch a view controller's outlets.)
I have to create a custom AlertView with a view that contain some label. I create the .h and .m of CustomAlert
this is the CustomAlert.h.
#import <Foundation/Foundation.h>
#interface CustomAlert: UIView
#property (nonatomic, weak) IBOutlet UILabel *ok;
#property (nonatomic, weak) IBOutlet UILabel *ok1;
#property (nonatomic, weak) IBOutlet UILabel *ok2;
#property (nonatomic, weak) IBOutlet UILabel *ok3;
#property (nonatomic, weak) IBOutlet UILabel *ok4;
#property (nonatomic, weak) IBOutlet UILabel *ok5;
#end
and this is the CustomAlert.m.
#import "CustomAlert.h"
#implementation CustomAlert
#synthesize ok = _ok;
#synthesize ok1 = _ok1;
#synthesize ok2 = _ok2;
#synthesize ok3 = _ok3;
#synthesize ok4 = _ok4;
#synthesize ok5 = _ok5;;
#end
I also create the xib with just a view and i connect the label with all the "ok".
Now, i want to add this View to an AlertView with just an Ok button. How can i do it? I want to use it with ios 7 and 6.
Thanks!!
In iOS 7 you should not munge a UIAlertView like this - and there is no need, because thanks to custom view transitions you can make your own small presented view in front of your interface, so that it looks and acts like a UIAlertView but can contain anything you like.
I've written an online tutorial on how to do this:
http://programming.oreilly.com/2014/01/transcending-uialertview-on-ios-7.html
And there's a downloadable github project that goes with it:
https://github.com/mattneub/custom-alert-view-iOS7
Just trying to make a simple WebView Mac app.
I've imported WebKit.h, declared a WebView #property, and #synthesized it in the .m, but when I go into IB and the connections tab, my outlet, say MyWebView, is not listed.
.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface MyAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet WebView *MyView;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain) IBOutlet WebView *MyView;
#end
.m
#synthesize MyView;
Latest version of Xcode. Thoughts?
Make sure that is defined like:
#property(nonatomic, retain) IBOutlet UIWebView *myWebView;
If you haven't added IBOutlet, IB won't show it as such.
After looking at the code you posted a little more carefully, I think the problem has to do with the name of your app delegate -- how (and why?) did you change it from the default AppDelegate? Is the blue cube in IB named AppDelegate or is it named MyAppDelegate? If it's the former, that's your problem -- change its class to MyAppDelegate in the Identity Inspector in IB.
See my comments on my original question. Error on my part.
I have seen the code below written 3 different ways (with regards to IBOutlet) Does it matter, I would say adding IBOutlet to both the declaration and the #property was more concise.
JUST PROPERTY:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SwitchViewController *switchViewController;
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
#end
JUST DECLARATION:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
#property(nonatomic, retain) UIWindow *window;
#property(nonatomic, retain) SwitchViewController *switchViewController;
#end
BOTH:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
#end
cheers gary
IBOutlet does only matter to InterfaceBuilder. For the compiler, UINibDeclarations.h #defines it to nothing.
InterfaceBuilder takes IBOutlet as a hint from the header file to list the available outlets for the class. If you wire up an object to an IBOutlet, no matter whether it is defined as a property or instance variable, this information is written into the nib.
When loading the nib, the loader tries to find the best possible way to setup the connection: First it tries to find a setter method with an appropriate name. If no such setter is found, it falls back to setting the instance variable directly, which is poor style, because memory management is not clear this way.
All your proposed examples have a property (and, of course, a setter method) of the right name. So in each case, the loader would use the setter method, no matter where the IBOutlet tag stands: There’s no difference between your examples, neither in the nib, nor in the way code is executed.
The best style would be to put the IBOutlet tag into the property definition.
Should not matter. With the 10.6 64-bit SDK you can also write the property without the ivar:
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
}
#property(nonatomic, retain) IBOutlet UIWindow *window;
#property(nonatomic, retain) IBOutlet SwitchViewController *switchViewController;
#end
The current style in Apple's example code puts the IBOutlet in the property declaration. For consistency, that's probably the best place to throw it.
I also found this way (without any property declaration):
#class SwitchViewController;
#interface iPhone_switcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
#end
What about this?
UIView.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface UIView : UIResponder {
IBOutlet UILabel *endLabel;
IBOutlet UIButton *goButton;
IBOutlet UITextField *textBox1;
IBOutlet UITextField *textBox2;
#property(nonatomic, retain) UILabel *endLabel;
#property(nonatomic, retain) UIButton *goButton;
#property(nonatomic, retain) UITextField *textBox1;
#property(nonatomic, retain) UITextField *textBox2;
}
- (IBAction)goButtonClicked;
#end
UIView.m
#import "UIView.h"
#implementation UIView
#synthesize textBox1, goButton;
#synthesize textBox2, goButton;
#synthesize textBox1, endLabel;
#synthesize textBox2, endLabel;
#synthesize goButton, endLabel;
- (IBAction)goButtonClicked {
}
#end
Going a bit crazy with the #synthesizes, are we? I do believe that your main problem here is that #property declarations need to be after the closing } of #interface.
I'm surprised the compiler didn't throw up a red flag the size of Greenland, tho'.
Additionally, you probably meant to make a custom subclass of UIView; I'll use MyView.
//MyView.m -- correct synthesize declaration
#synthesize textBox1, goButton, textBox2, endLabel;
//MyView.h -- correct interface declaration
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface MyView : UIView {
IBOutlet UILabel *endLabel;
IBOutlet UITextField *textBox1;
IBOutlet UITextField *textBox2;
IBOutlet UIButton *goButton;
}
#property(nonatomic, retain) UIButton *goButton;
#property(nonatomic, retain) UILabel *endLabel;
#property(nonatomic, retain) UITextField *textBox1;
#property(nonatomic, retain) UITextField *textBox2;
#end
The first problem is that you're naming your class UIView, which already exists in UIKit. See #Williham's advice on resolving this.
You only need one #synthesize per property, and when the property name matches the instance variable name, you should only need to do something like this in your .m file:
#synthesize endLabel;
#synthesize goButton;
#synthesize textBox1;
#synthesize textBox2;
Also, you're likely to run into problems getting your IBAction method to work. To use a method for a target-action linkage, it must have a return type of IBAction (which you have correct) and accept an id parameter representing the sender. The canonical method signature looks like this:
- (IBAction) goButtonClicked:(id)sender;
I'd actually recommend a method name that's not explicitly tied to the button that invokes it, especially since there could be other ways to invoke the same action. (For example, if you were writing a desktop application, a key equivalent or menu command could do the same thing.)