Retrieving Custom Button Property in Objective-C - objective-c

I've created a custom button called TaskUIButton that inherits from UIButton. The only difference I have right now is a "va" property.
Here's the interface
// TaskUIButton.h
#interface TaskUIButton : UIButton
{
NSString *va;
}
#property(nonatomic, retain) NSString *va;
#end
And the implementation file
//TaskUIButton.m
#implementation TaskUIButton
#synthesize va;
#end
Now, I've got an action that I'm using which I want to use to set and retrieve the va property of a button (just for testing/experimentation of course).
Here is where the button action is
- (IBAction)setAndRetrieveVa:(id)sender{
TaskUIButton *imaButton = [TaskUIButton buttonWithType:UIButtonTypeRoundedRect];
imaButton.va = #"please work";
NSLog(#"%#", imaButton.va);
}
Upon activating the setAndRetrieveVa: action, my app crashes with:
-[UIRoundedRectButton setVa:]: unrecognized selector sent to instance 0x4b3a5a0
I'm sure that its a stupid mistake on my part, but I've been going at it for a while and would love some insight!
Thanks!

You are getting this because buttonWithType: is returning a new object which is a UIRoundedRectButton object which is a subclass of UIButton. You can't alter this behavior of the method unless you override but you are unlikely to get what you want. You should take the alloc-init approach.
Using Associative References
You will need to #import <Foundation/NSObjCRuntime.h> for this to work.
To set,
objc_setAssociatedObject(button, "va", #"This is the string", OBJC_ASSOCIATION_RETAIN);
And to retrieve,
NSString * va = (NSString *)objc_getAssociatedObject(button, "va");
This way you wouldn't need to subclass UIButton.

I ended up just extending UIControl...turned out to be alot easier :)
- (IBAction)setAndRetrieveVa:(id)sender{
TaskUIButton *newTaskButton = [[TaskUIButton alloc] initWithFrame:CGRectMake(29.0, (76.0+ (88*taskCounter)), 692, 80.0)];
[newTaskButton addTarget:self action:#selector(function1:)forControlEvents:UIControlEventTouchUpInside];
[newTaskButton addTarget:self action:#selector(function2:) forControlEvents:UIControlEventTouchDragExit];
[newTaskButton setBackgroundColor:[UIColor grayColor]];
[newTaskButton setTitle:#"0" forState:UIControlStateNormal];
[newTaskButton setVa:#"please work!"];
NSLog(#"%#", newTaskButton.va);
}
And for click highlighting, I can always add a function that changes the background color when touch-down occurs, and switches the color back when touch-up occurs. Hurrah!

Related

How to create a custom NSView for NSSavePanel in Cocoa MacOS objective C?

I need to add a save extension selector with a text label next to it to my NSSavePanel. In the screenshot attached I try to demonstrate that I succeeded in adding an NSComboBox to my panel with the function setAccessoryView. However I have no idea how to create a custom NSView, which includes both an NSComboBox and an NSTextView or equivalent. I found no tutorials on the internet (or if I found one it was extremely outdated) showing how to create custom NSViews in objective-C in Cocoa on MacOS.
How can I create a custom NSView containing a combobox and a text label? Or how can I add two "stock" NSViews to the same NSSavePanel? Please be as detailed in your answer as possible, as I have very limited objective-c experience.
You asked how to create an NSView in Objective-C with an NSTextField and an NSComboBox as subviews.
Basically, you could define them in Interface Builder and programmatically set the resulting view in Objective-C as the accessoryView of the NSSavePanel. Alternatively, the custom NSView could be created entirely in Objective-C, which is probably the easier option here.
After instantiating an NSView, you can use addSubview: to add an NSTextField and an NSComboBox accordingly. Then you can use NSLayoutConstraints to set up Auto Layout, which takes care of sizing the accessoryView and arranging the subviews properly based on the width of the dialog.
If you create the views programmatically and use Auto Layout, you must explicitly set translatesAutoresizingMaskIntoConstraints to NO.
Should you want to set the allowedContentTypes, a textual mapping of the displayed extension to UTType via a NSDictionary might be useful.
If you set the delegate of the NSComboBox to self, then you will be informed about changes of the user selection in the NSComboBox via comboBoxSelectionDidChange:.
If the things discussed are implemented appropriately in code, it might look something like this for a self-contained example:
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#import "ViewController.h"
#interface ViewController () <NSComboBoxDelegate>
#property (nonatomic, strong) NSSavePanel *savePanel;
#property (nonatomic, strong) NSDictionary<NSString *, UTType*> *typeMapping;
#end
#implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
_typeMapping = #{
#"jpeg": UTTypeJPEG,
#"png": UTTypePNG,
#"tiff": UTTypeTIFF
};
}
return self;
}
- (NSView *)accessoryView {
NSTextField *label = [NSTextField labelWithString:#"Filetypes:"];
label.textColor = NSColor.lightGrayColor;
label.font = [NSFont systemFontOfSize:NSFont.smallSystemFontSize];
label.alignment = NSTextAlignmentRight;
NSComboBox *comboBox = [NSComboBox new];
comboBox.editable = NO;
for (NSString *extension in self.typeMapping.allKeys) {
[comboBox addItemWithObjectValue:extension];
}
[comboBox setDelegate:self];
NSView *view = [NSView new];
[view addSubview:label];
[view addSubview:comboBox];
comboBox.translatesAutoresizingMaskIntoConstraints = NO;
label.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[
[label.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:-12],
[label.widthAnchor constraintEqualToConstant:64.0],
[label.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:0.0],
[comboBox.topAnchor constraintEqualToAnchor:view.topAnchor constant:8.0],
[comboBox.leadingAnchor constraintEqualToAnchor:label.trailingAnchor constant:8.0],
[comboBox.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:-8.0],
[comboBox.trailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:-20.0],
]];
return view;
}
- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
NSComboBox *comboBox = notification.object;
NSString *selectedItem = comboBox.objectValueOfSelectedItem;
NSLog(#"### set allowedContentTypes to %# (%#)", selectedItem, self.typeMapping[selectedItem]);
[self.savePanel setAllowedContentTypes:#[ self.typeMapping[selectedItem] ]];
}
- (IBAction)onSave:(id)sender {
NSWindow *window = NSApplication.sharedApplication.windows.firstObject;
self.savePanel = [NSSavePanel new];
self.savePanel.accessoryView = [self accessoryView];
[self.savePanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse result) {
if (result != NSModalResponseOK) {
return;
}
NSURL *fileURL = self.savePanel.URL;
NSLog(#"### selectedFile: %#", fileURL);
}];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
#end
Finally, a screenshot of the above demo code in action looks like this:
Press Cmd-N to add a new file to your project. Choose a View file to add a xib file that has a custom view.
Open the xib file and add the controls to the custom view. Press the Add button in the project window toolbar to access the user interface elements.
Use the NSNib class to load the xib file and get the custom view.

UIButton created in code not working properly

So I've been holding off asking this question for a while because I know the solution will most likely be something very very simple. But I have come to the end of my tether so here goes:
I have created a UIButton programatically and linked it to a method, but it is not working!!
.h definition
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface CreaterPage : UIViewController
{
IBOutlet UIView *postcardView;
IBOutlet UIButton *returnButton;
}
-(void)goBack:(UIButton *)button;
#end
.m definition
#import "CreaterPage.h"
#implementation CreaterPage
-(void)viewDidLoad
{
NSLog(#"Creater Page View Loaded Successfully");
UIButton *goHomeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[goHomeButton addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchDown];
[goHomeButton setTitle:#"Go Back" forState:UIControlStateNormal];
goHomeButton.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:goHomeButton];
}
-(void)goBack:(UIButton *)button
{
NSLog(#"Home");
}
#end
And basically, when I run the code, the button appears as defined but my program crashes whenever I press it.
In the main.m file, it gives me the error
Thread 1: Program received signal: "EXC_BAD_ACCESS".
On the line
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
I've tried all sorts and only turned to creating it programatically because I couldn't get it working through the interface builder.
So I'm hoping somebody on here can change my juvenile ways and show me where I'm going wrong :D
Thanks,
Matt
EDIT 1: Changed #selector(goBack) to #selector(goBack:)
My first guess would be that your action is defined as such:
[goHomeButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchDown];
Note the #selector(goBack) without a colon following the method name. Your method in your .m file is defined as:
-(void)goBack:(UIButton *)button
So I imagine changing your action to #selector(goBack:) would clear things up.
Sidenote: It's very uncommon to define the type of the sender for an IBAction, as you have done. While you might not encounter any issues as long as your UIButton is the only UI object that causes the method to be called, it's very poor practice. You should change your method signature to:
-(IBAction)goBack:(id)sender
Note also the use of IBAction in place of void. While they're syntatically the same thing, the IBAction makes it clear to readers, and to Interface Builder, which methods are available for linking.
Change
[goHomeButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchDown];
to
[goHomeButton addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchDown];

Displaying an UIImageView property from another class programmatically

Well, here's the situation:
I've got...
a custom class with an UIImageView-property, let's call it the Enemy-class
a ViewController
a NSMutableArray to help create multiple instances of Enemy (called enemies)
and what I want:
be able to create an unlimited amount of Enemy-Instances through a method in my ViewController (like [self spawnEnemy];, where self is the ViewController)
and, subsequently, display the UIImageView property (let's call it "enemyImage") on the view that is controlled by my ViewController
I've tried something like this:
-(Enemy *) spawnEnemy
{
Enemy *tempEnemy = [Enemy new];
[enemies addObject:(Enemy*)tempEnemy];
[self.view addSubview:(UIImageView*)[[enemies objectAtIndex:[enemies count]] enemyImage]];
//randomLocation is declared in the Enemy-Class and just assigns a random
//CGPoint to self.enemyImage.center
[[enemies objectAtIndex:[enemies count]] randomLocation];
return [[enemies objectAtIndex:[enemies count]]createEnemy];
}
This runs without errors, randomLocation gets called (tried with NSLog), AND if I do something like this in another Method of ViewController:
[[self spawnEnemy] enemyTestMethod];
enemyTestMethod is being executed as well.
But still, no enemieViews are displayed on the screen...
What am I doing wrong?
Thank you so much for your help and time.
==== Edit ====
Here's the relevant code from Enemy.h/Enemy.m:
#interface Enemy : NSObject
{
UIImageView *enemyImage;
}
#property (nonatomic, retain) IBOutlet UIImageView *enemyImage;
-(Enemy*) createEnemy;
//Enemy.m
#implementation Enemy
#synthesize enemyImage, speed;
-(Enemy *) createEnemy
{
self.enemyImage = [[UIImageView alloc] init];
[self.enemyImage setImage:[UIImage imageNamed:#"enemy.png"]];
return self;
}
I also corrected the last line in the spawnEnemy-Method to properly send createEnemy.
You don't include the code in Enemy where you alloc/init the UIImageView property. Unless this code explicitly specifies a CGRect with the size and origin that you want, the view will be initialized with CGRectZero, which means even if you're correctly adding the subview (and it looks like you are) you still won't see it anywhere.
Post the Enemy code, and the problem will probably be immediately apparent.
Have you called -createEnemy before you added them to your view?
OK, you've got this checked.
Then maybe you should check as #MusiGenesis suggested.
To do this, you need to inspect the properties of your enemyImage.
You can do this in either of the following ways:
print its frame.size by:
NSLog(#"enemyImage frame size: %#", NSStringFromCGRect(enemy.enemyImage));
set a breakpoint where you feel great, and check with your debugger:
p (CGRect)[[enemy enemyImage] frame]

Protocol Memory

I'm sorry for repost. What have really bug me is if property retain should release when:
case 1 : (code below) button is already alloc in init: then add to subview then release it, and by any chance I set the button property in another class (I didn't release in dealloc:) it will leak then?
case 2 : button is already alloc in init: then add to subview then release it, and by any chance I didn't set the any button property in another class (I didn't use the property) (I release it in dealloc) then it will crash.
So what should I do if I want to alloc button in init: and I want to set the property too ?
#interface SomeClass : UIView {
UIButton *_button;
}
#property (nonatomic, retain)UIButton *button;
#implementation SomeClass
#synthesize button = _button;
- (id)init {
_button = [[UIbutton alloc] initWithFrame:CGRectMake(0.0f,0.0f,100.0f,20.0f)];
[[_button titleLabel] setFont:BUTTON_FONT];
[_button setBackgroundImage:[[UIImage imageNamed:#"button_blue.png"] stretchableImageWithLeftCapWidth:20.0f topCapHeight:15.0f] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
[[_button titleLabel] setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[_button addTarget:self action:#selector(buttonDidTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubView:_button];
[_button release];
}
- (void)dealloc {
//[self.button release]; // case 1
[self.button release]; // case 2
[super dealloc];
}
#end
So what should I do if I want to alloc button in init: and I want to set the property too ?
Here's how your code should look:
#implementation SomeClass
#synthesize button = _button;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
UIButton *button = [[UIbutton alloc] initWithFrame:CGRectMake(0.0f,0.0f,100.0f,20.0f)];
// Setup the button here...
[self addSubView:button];
self.button = button;
[button release];
}
return self;
}
- (void)dealloc {
[_button release];
[super dealloc];
}
#end
Changes I made:
initWithFrame: is the designated initializer of the UIView, so that's the init method you've got to override
Always check to make sure that your superclass initialized successfully before you setup your class
You've got to return self at the end of your init statements. I don't think your code would have compiled as written.
You created a property, you should use it. Use a temp variable to do all the setup for your button, then when you're finished with setup, use the property accesssor to set the variable and release your temp variable.
Because you only use the property to get/set your button, when it's time to dealloc you can guarantee that the _button iVar will either be valid or nil. Either way calling release on that variable is OK.
I don’t understand what you mean by “using it for read-only”. As declared, the property is read-write both for the class itself and from the outside. But the question can be reasonably answered nevertheless – once your class retains some object, it must release it when it gets deallocated.
P.S. I think you can safely drop the underscore prefix for private variables, it serves no real purpose and makes you write more code. In fact, with modern runtimes you can even drop the instance variable declaration:
#interface Foo : NSObject {}
#property(assign) BOOL bar;
#end
#implementation Foo
#synthesize bar;
#end
OK, third attempt: The problem is that you are in fact setting the property by assigning to the instance variable. Properties and instance variables are closely tied, when you give the following declaration…
#synthesize button = _button;
…you are saying that the _button instance variable should be used to store the value of the button property. Which means that your code over-releases, since you alloc the button in init (+1), release the button in init (-1) and then release again in dealloc (-1 again).
If you have not yet studied the Cocoa Memory Management Guide, do it. Even if you don’t plan to read any other documentation (which would be a pity), make an exception with this one, it will pay you back plenty.
sure, You should release it, because you have used retain for it.
In your -init method, you have a balanced retain/release call. So you don't need to release it again. But by doing it, you are sacrificing the reliability of the value held by _button. If somewhere down the lane the button is removed from the subviews, the button's retainCount could hit zero and it can be deallocated. Then the value held by _button is garbage. So you should not release _button in -init and rather you should do that in -dealloc.
Now, if you access the property elsewhere (outside this UIView object), you don't need to release it again unless you retain it there.
Try to replace [self.button release]; with [self.button removeFromSuperview];

Setting a ViewController's properties after instantiation

I'm creating an instance of a viewController, and then trying to set the text on of it's properties, a UILabel.
BoyController *boyViewController = [[BoyController alloc] initWithNibName:#"BoyView" bundle:nil];
NSString *newText = [astrology getSignWithMonth:month withDay:day];
boyViewController.sign.text = newText;
NSLog(#" the boyviewcontroller.sign.text is now set to: %#", boyViewController.sign.text);
[newText release];
I tried this, but it didn't work...
So I tried the following:
BoyController *boyViewController = [[BoyController alloc] initWithNibName:#"BoyView" bundle:nil];
UILabel *newUILabel = [[UILabel alloc] init];
newUILabel.text = [astrology getSignWithMonth:month withDay:day];
boyViewController.sign = newUILabel;
NSLog(#" the boyviewcontroller.sign.text is now set to: %#", newUILabel.text);
[newUILabel release];
But no avail..
I'm not sure why I can't set the text property of the UILabel "sign" in boyViewController..
The problem here is that the initializer does not actually load the nib file into memory. Instead, loading the nib is delayed until your application requests the view controller's view property. As such, your controller's sign property is null when you access it.
Manually requesting the controller's view property would make your example work...
BoyController *boyViewController = [[BoyController alloc] initWithNibName:#"BoyView" bundle:nil];
[boyViewController view]; // !!!: Calling [... view] here forces the nib to load.
NSString *newText = [astrology getSignWithMonth:month withDay:day];
boyViewController.sign.text = newText;
// and so on...
However, I'd guess that what you're really trying to do is create and configure your view controller before setting it free to do it's own thing. (Perhaps to display it modally, say.) Calling [... view] manually is not going to be a long-term solution.
Better is to set a separate property on your view controller for the label text and then implement viewDidLoad to assign it to the label:
#interface BoyViewController : UIViewController {
IBOutlet UILabel *label;
NSString *labelText;
}
#property(nonatomic, copy)NSString *labelText;
#end
#implementation
#synthesize labelText;
- (void)viewDidLoad
{
[label setText:[self labelText]];
}
// and so on...
#end
This has the added benefit of your label text being reset in case the view is purged during a low memory event.
Did you bind your outlets at Interface Builder?
It seems that you need to bind sign outlet of the first example into Interface Builder in order to actually set that text to whatever you want.
Once you bind your outlet to the actual UI component at Interface Builder, then you should be able to do something like:
NSString *newText = [astrology getSignWithMonth:month withDay:day];
[[boyViewController sign] setText:newText];
This is what you need to know about binding.
Your second example does not make sense at all to me.