Is it possible to use a string variable to refer to a UIImageView variable and change the image? The string variable is created by adding an "s" to the start of the tag of the button pressed. This is also the name of a specific UIImageView.
So one of the UIImageView declared in the .h file looks like this:
#property (retain, nonatomic) IBOutlet UIImageView *s11;
And in the .m file so far:
NSString *lastpressnum = [#(sender.tag) stringValue];
NSString *lastpressed = [NSString stringWithFormat:#"%s%#", "s", lastpressnum];
[lastpressed setImage:image];
KVC is the tool you need.
UIImageView * theImageView = [self valueForKey:lastpressed];
[theImageView setImage:image];
Related
What I want to do is when user select a district from UITableView in Swift, I'm taking the district name and add .png to it and send the image name variable into Objective-C file and inside it I'm creating UIImageView.
This is my code inside my Objective-C .h file,
#property (weak, nonatomic) NSString *mapImage;
This is how I assign value to it from Swift UITableView when user click on UITableView (didSelectItemAtIndexPath),
selectesState = namesArray[indexPath.row]
let temp :String = selectesState + ".png"
let instanceOfCustomObject: StateMapPinView = StateMapPinView()
instanceOfCustomObject.mapImage = temp
This is the Objective-C code I'm setting the image in .m file (ViewDidLoad),
UIImage *image = [UIImage imageNamed:_mapImage];
_myView = [[VIPhotoView alloc] initWithFrame:self.view.bounds andImage:image];
_myView.autoresizingMask = (1 << 6) -1;
[self.view addSubview:_myView];
But it keeps returning null value.
Whats wrong with my code?
You have to change the declaration of the property to:
#property (copy, nonatomic) NSString *mapImage;
This will make sure that value is copied when assigned. In your case you use weak and therefore the value is deallocated at the end of the execution of the current scope.
Makesure , you have the same 'instanceOfCustomObject' instance.
If it is instantiated again, then you will get null for 'mapImage'.
Use the same instance of 'instanceOfCustomObject' and try in your Obj-c.
CustomObjectClass *instanceOfCustomObject = (CustomObjectClass *) 'same_instanceOfCustomObject';
[UIImage imageNamed: instanceOfCustomObject.mapImage];
For some reason the image is not showing with this code:
YellowClass.h
#property (strong, nonatomic) IBOutlet UIImageView *myImage;
BlueClass.m
YellowClass *yellowClass = [[YellowClass alloc] init];
yellowClass.myImage.image = [UIImage imageNamed:#"Img.png"];
Two things. The instance of YellowClass that you create with alloc init is probably not the one you have on screen (though I can't be sure with that small snippet of code). Secondly, the view of yellowClass has not yet been loaded at the time you access its IBOutlet (myImage), so that outlet will be nil.
I am just start studying iOS developing watching Stanford iOS course, but it looks like I have already missing something.
I have a form with UILabel and UIButton. When an user press the button the title of the button must be added to the text of label.
Here is my current CalculatorViewController.h:
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *display;
#end
and here is a CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
#synthesize display = _display;
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [sender currentTitle];
UILabel *myDisplay = self.display;
myDisplay.text = [myDisplay.text stringByAppendingString:digit];
}
#end
The problem is that self.display (and myDisplay) variables have a nil value. Looks like I need to do something to link my variable with control on the form. What ?
Thanks in advance.
You need to link the control, the UILabel in Interface Builder to the variable in your CalculatorViewController class.
It is very likely that the file's owner (talking about the Xib file) is your CalculatorViewController, so you need to Control+drag the file's owner (or the object representing your VC) to the control and you will be shown a menu with the possible IBOutlet variables declared in your class, so you select the one you want to represent the control.
You can check if this link is properly set in two ways: (1) Select the UILabel in Interface Builder and see if there's a connection to the variable in the Connections Inspector, or (2) In your code you'll see a bullet near the IBOutlet declaration, if the bullet is hollow the connections is not set, if the bullet is filled the connection is set.
There is no need of this line
UILabel *myDisplay = self.display;
You have already declared your label in your interface file
- (IBAction)digitPressed:(UIButton *)sender
{
NSMutableString *string = [myDisplay.text stringByAppendingString:sender.titleLabel.text];
myDisplay.text = string;
}
I am trying to create a UIImageView but I have to make it programmatically and I have to be able to declare it with an instance variable (in the .h file or something of the sort). Here is the code for creating it; however, this does not allow me to use it in other methods.
UIImageView *airImage = [[UIImageView alloc]
initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];
I have looked on other people asking similar questions however none of them will allow me to create an instance variable. BTW that code is in my viewDidLoad. Thanks in advance!
In your .h use:
UIImageView *airImage;
In your viewDidLoad:
airImage=[[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];
Or you can declare it as a property:
#property (nonatomic, strong) UIImageView *airImage;
and use to access it:
self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
To be more specific, instance variables should be created in a specific place in an interface (can be both in your .h and .m files, but use .h as it is more common).
If you want to declare it in your .h file, then you will want your code to look like this:
#interface ClassName : UIViewController {
UIImageView *_airImage; //many developers use _ to represent ivars
}
#end
To set the value of the variable, then you can use
_airImage = [[UIImageView alloc]init...];
Property's are another option. Instead, you can declare this like so:
#interface ClassName : UIViewController
#property (strong, nonatomic) UIImageView *airImage;
#end
To set this value, simply use,
self.airImage = [[UIImageView alloc]init...];
Hope this helped clear some things up. Use this question to help understand the difference and when to use ivars vs properties: What is the difference between ivars and properties in Objective-C
This tutorial shows how you can use both ivars and properties together, and just help you understand them both better: http://www.icodeblog.com/2011/07/13/coding-conventions-ivars/
in your .h
#property (nonatomic, strong) UIImageView *airImage; // public
in your .m (viewDidLoad or wherever you want to init your ImageView)
self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
I want to modify a label in objective C like this:
When pushing a button named "Car" I want the label "pushedCar" to unhide.
Right now it looks like this but its static...
if ([buttonName isEqualToString:#"Car"]) {
self.pushedCar.hidden = NO;}
How can I write something like:
if ([buttonName isEqualToString:#"Car"]) {
self.pushed%#.hidden , buttonName = false;}
It's kind of a stupid example but I need it for something too complex to write down here.
Thanks in advance.
Michael
Change all your button titles like, Car, Van , Bus etc.
then make this as touch up inside method of all them,
-(IBAction) buttonPressed:(UIButton *) pressedButton{
NSString *buttonName = pressedButton.titleLabel.text;
UILabel *label = [self valueForKey:[NSString stringWithFormat:#"pushed%#", buttonName]];
label.hidden = NO;
}
like this?
if ([ButtonName isEqualToString:#"Car"]) {
self.pushedCar.hidden = !self.pushedCar.hidden;
}
use KVC
id name = nil
if(myButtonCurrentTitle isEqualTo:#"Car"]) name = #"Car";
assert(name);
UILabel *label = [self valueForKey:[NSString stringWithFormat:#"pushed%#", name]];
assert([label isKindOfClass:[UILabel class]]);
If you mean "How could I get an object that is related to a string" it would probably be simplest to put the buttons into an NSDictionary so you could do something like
MyButton *button = [buttonDict objectForKey:#"Car"];
but to be honest, your example code doesn't make much sense so I'm finding it hard to know how to write a decent example for you
If I understand it correctly, you want different buttons and labels (e.g. car, horse, cat).
In that case make an NSDictionary with the key being the buttonName (#"Car") and the the object for it the label.
You can write then something like:
UILabel *label = [myDictionary objectForKey:buttonName];
label.hidden = NO; // or label.hidden = !label.hidden;
Hope this helps!
I think you are trying to use Key Value Coding to get the button instance based on some arbitrary string.
You firstly need to expose the button instances using properties:
#interface MyClass : UIView
{
// These are connected using Interface Builder
IBOutlet UIButton *carButton;
IBOutlet UIButton *busButton;
IBOutlet UIButton *bikeButton;
}
#property (string, nonatomic, readonly) UIButton *carButton;
#property (string, nonatomic, readonly) UIButton *busButton;
#property (string, nonatomic, readonly) UIButton *bikeButton;
#end
And you can get the instance of the button using:
NSString *thing = "#car";
UIButton *button = [self valueForKey:[NSString stringWithFormat:#"%#Button", thing]];
[button setHidden:YES];