Use UISwitch to Show/Hide Image - uiimageview

I am trying to use a UISwitch to show an image when on, and hide that image when off. I keep running into the issue where the image shows when the switch is turned on but it doesn't hide when the switch is turned off. I've tried a bunch of different solutions online but none seem to work.
This is one of the solutions I've tried. Is there another way to determine if the switch is on aside from switchname.on? Maybe if else statements aren't the way to go?
Any help is greatly appreciated!
#property(nonatomic, strong) UIImageView *spawnImage;
- (IBAction)changeSwitch:(id)sender{
UIImageView *spawnImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 70, 300, 169)];
if(spawnPoints.on){
NSLog(#"Switch is ON");
[spawnImage setImage:[UIImage imageNamed:#"freight-spawn.png"]];
}
else{
NSLog(#"Switch is OFF");
[spawnImage setImage:[UIImage imageNamed:#"freight-none.png"]];
}
[self.view addSubview:spawnImage];
}
Update!
Thanks to #meda we figured out a solution.
.h
#interface ViewController: UIViewController {
IBOutlet UIImageView *spawnImage;
}
.m
-(IBAction)changeSwitch:(id)sender{
if(spawnPoints.on){
NSLog(#"Switch is ON");
[spawnImage setImage:[UIImage imageNamed:#"freight-spawn.png"]];
}
else{
NSLog(#"Switch is OFF");
[spawnImage setImage:[UIImage imageNamed:#"freight-none.png"]];
}
[self.view addSubview:spawnImage];
}
Note that this is an IB solution where a blank UIImageView must be dragged in and position and hooked up to the outlet in connections.

Is there another way to determine if the switch is on aside from switchname.on?
Yes you can try isOn instead of on:
if(spawnPoints.isOn){
NSLog(#"Switch is ON");
[spawnImage setImage:[UIImage imageNamed:#"freight-spawn.png"]];
}
else{
NSLog(#"Switch is OFF");
[spawnImage setImage:[UIImage imageNamed:#"freight-none.png"]];
}
or
[spawnImage setImage:[UIImage imageNamed:spawnPoints.isOn?
#"freight-spawn.png": #"freight-none.png"]];

Related

UIButton in a subClass of UIViewController not respond to any touches

I have met the problem some days ago,I also have searched the solutions everywhere,but none work fine.I add a button into a custom viewController(is a subClass of UIViewController),then,add the customView into myMainViewController.
code like this:
customView.h
cusomView:UIViewController
{
UIButton *myButton;
}
-(void)doSomething:(id)sender;
customView.m
-(void)viewDidLoad
{
[super viewDidLoad];
self.view=[UIView alloc] initWithFrame:CGRectMake(0,100,100,100);
//init the button....
[myButton addTarget:self action:#selector(doSomething:) forControlEvents:...upInside];
[self.view addSubViews:myButton];
}
-(void)doSomething:(id)sender
{
.......
}
myMainViewController
-(void)viewDidLoad
{
......
customView *cusView=[customView alloc] init];
[self.view addSubViews:cusView];
}
just so,It display well,But the button do not repond to any touches.Could you help me?thanks.
Is there a reason why you aren't adding the uibutton to the myMainController directly?

How I tell my UITextField (code) to resign first responder?

I have created my UITextField by code, without InterfaceBuilder. I want the keyboard to disappear when the button "Done" is pushed. How does the code know that I am referending to an UITextField and no to other one
First, thanks a lot.
My code is like this:
-(void)viewDidLoad {
[super viewDidLoad];
field = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 185, 30)];
field.adjustsFontSizeToFitWidth = YES;
field.textColor = [UIColor blackColor];
field.placeholder = #"Text";
field.keyboardType = UIKeyboardTypeDefault;
field.returnKeyType = UIReturnKeyDone;
field.backgroundColor = [UIColor blueColor];
field.delegate = self;
[self.view addSubview:field];
}
......
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
With this code I push the button Done and nothing happen. Is like that how you say?
Edit:
I've created two UITextField how I did with the previous one. But now, for each row I do this:
if ([indexPath row] == 0) {
[cell.contentView addSubview:pTextField];
}
else {
[cell.contentView addSubview:pTextField];
}
So with this code, the program received signal "EXC_BAD_ACCESS". Any idea why happen this?
How does the code know that I am referending to an UITextField and no to other one
Your textFieldShouldReturn: method's textField parameter will always be the text field that is currently active.
The method has to return a BOOL, you should be getting compiler warnings with it as it stands. Try
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Note that you are also currently leaking memory in the way you add the text field. You should set it as a property as per WrightCS's answer so that you can refer to it later on. So at the end of your viewDidLoad:
self.myTextField = field;
[field release];
Define your textField in your header, then you can use the following:
.h
#interface MyeViewController : UIViewController <UITextFieldDelegate>
{
UITextField * myTextField;
}
#end
.m
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[myTextField resignFirstResponder];
/* textField here is referenced from
textFieldShouldReturn:(UITextField *)textField
*/
}
Make sure you set the delegate of your programatically created UITextField to self (the view controller that created the object) and implement the appropriate UITextFieldDelegate method (I think its textFieldShouldReturn:) and call in that method resignFirstResponder on the textField argument passed to the delegate method (which will be your UITextField).

Xcode - UILabel is not changing value on viewDidAppear

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;

How to set or change button background color on IBAction

I try to set the background color of a button, but, isn't work. Some can helpme please?
- (IBAction)OrBoton:(id)sender {
[sender setBackgroundColor:(NSColor *)redColor];
}
But, if I try to setTitle, this works fine.
Is unacessible the Backgroundcolor property?
I don't recived any error when I Build and Debug, but don't works.
Thanks!
Try this
- (IBAction)OrBoton:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setBackgroundColor:[UIColor redColor]];
}
Well, finally, I opted for change image button from a colored image, because unfortunately, at the moment, this is impossible for me.
Try This.
- (IBAction)Click:(UIButton *)sender
{
if (sender.selected)
{
sender.selected = NO;
sender.backgroundColor=[UIColor whiteColor];
}
else
{
sender.selected = YES;
sender.backgroundColor=[UIColor blueColor];
}
}

xcode Removing Some Subviews from view

Greetings all,
I am a noob and I have been trying to work through this for a few days.
I am adding images to a view via UItouch. The view contains a background on top of which the new images are add. How do I clear the images I am adding from the subview, without getting rid of the UIImage that is the background. Any assistance is greatly appreciated. Thanks in Advance.
here is the code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps==2) {
imageCounter.text =#"two taps registered";
//__ remove images
UIView* subview;
while ((subview = [[self.view subviews] lastObject]) != nil)
[subview removeFromSuperview];
return;
}else {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGRect myImageRect = CGRectMake((touchPoint.x -40), (touchPoint.y -45), 80.0f, 90.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"pg6_dog_button.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
[imagesArray addObject:myImage];
NSNumber *arrayCount =[self.view.subviews count];
viewArrayCount.text =[NSString stringWithFormat:#"%d",arrayCount];
imageCount=imageCount++;
imageCounter.text =[NSString stringWithFormat:#"%d",imageCount];
}
}
What you need is a way of distinguishing the added UIImageView objects from the background UIImageView. There are two ways I can think of to do this.
Approach 1: Assign added UIImageView objects a special tag value
Each UIView object has a tag property which is simply an integer value that can be used to identify that view. You could set the tag value of each added view to 7 like this:
myImage.tag = 7;
Then, to remove the added views, you could step through all of the subviews and only remove the ones with a tag value of 7:
for (UIView *subview in [self.view subviews]) {
if (subview.tag == 7) {
[subview removeFromSuperview];
}
}
Approach 2: Remember the background view
Another approach is to keep a reference to the background view so you can distinguish it from the added views. Make an IBOutlet for the background UIImageView and assign it the usual way in Interface Builder. Then, before removing a subview, just make sure it's not the background view.
for (UIView *subview in [self.view subviews]) {
if (subview != self.backgroundImageView) {
[subview removeFromSuperview];
}
}
A more swiftly code for approach #1 in only one functional line of code :
self.view.subviews.filter({$0.tag == 7}).forEach({$0.removeFromSuperview()})

Resources