Array of UIButtons, can't change button image - objective-c

I have an array of UIButton objects (I have 15 buttons so I wanted to store them in an array for easy processing). I want to be able to iterate through the array and change the background image/image that the button uses. This is how I currently have it set up:
#property (nonatomic, weak) UIImage *greenLight;
#property (nonatomic, weak) UIImage *yellowLight;
#property (nonatomic, weak) UIImage *redLight;
#property (weak, nonatomic) IBOutlet UIButton *task1Button;
#property (weak, nonatomic) IBOutlet UIButton *task2Button;
#property (weak, nonatomic) IBOutlet UIButton *task3Button;
#property (nonatomic, copy) NSMutableArray *buttonArray;
- (instancetype)init
{
self = [super init];
if(self){
[self.buttonArray addObject:self.task1Button];
[self.buttonArray addObject:self.task2Button];
[self.buttonArray addObject:self.task3Button];
self.greenLight = [UIImage imageNamed:#"greenlight.ICO"];
self.yellowLight = [UIImage imageNamed:#"yellowlight.ICO"];
self.redLight = [UIImage imageNamed:#"redlight.ICO"];
NSLog(#"init complete...");
}
return self;
}
Then I have the following in viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.task1Button setImage:self.redLight forState:UIControlStateNormal];
[(UIButton *)self.buttonArray[1] setImage:self.greenLight forState:UIControlStateNormal];
}
The second line in viewWillAppear works and changes the button's image. The third line, however, does not. The debugger shows the code reaching that line and not throwing any errors, so I don't know why setting the button image when the button is in an array would not work.
Any ideas?

Try replacing last line in your answer with this :
[(UIButton *)[self.buttonArray objectAtIndex:1] setImage:self.greenLight forState:UIControlStateNormal];
But i'm doing this without compiler so i'm not really sure about where to place the first bracket [
Tell me if it works, i'm not totally sure about it.

Related

UILabel in detail view is not being populated

I have a UITableView populated with an array of data gathered from an SQLite database. When a cell is selected a detail view is loaded relevant to that cell. The title on the navigation controller changes ok but a UILabel on the detail view does not get populated.
Relevant tableViewController code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
ExhibitorDetailViewController *exhibitorDetailViewControllerInstance = [[ExhibitorDetailViewController alloc] init];
exhibitorDetailViewControllerInstance.theTitle = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
exhibitorDetailViewControllerInstance.exhibitorDetailModal = [arrayOfExhibitors objectAtIndex:indexPath.row];
// Have tried setting the text before the detail view is loaded wiht the line below
exhibitorDetailViewControllerInstance.LabelExhibitorDescription.text = arrayOfExhibitors.description;
[self.navigationController pushViewController:exhibitorDetailViewControllerInstance animated:YES];
}
The viewDidLoad event of the detail view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = _theTitle; //This sets the title ok
NSLog(#"0 - %#", _exhibitorDetailModal.description); //This property is what I want to fill the UILabel with
self.LabelExhibitorDescription.text = _exhibitorDetailModal.description; // This does nothing
self.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.8 blue:0.8 alpha:2.0f]; // If I don't set the background colour here it loads up black (despite being set in the storyboard)
NSLog(#"label text = %#", self.LabelExhibitorDescription.text); // This shows the text box being null despite having its text property set above
}
Detail view header file:
#interface ExhibitorDetailViewController : UIViewController
{
}
#property (nonatomic, retain) NSString *theTitle;
#property (strong, nonatomic) IBOutlet UILabel *nameLabel;
#property (strong, nonatomic) IBOutlet UILabel *categoryLabel;
#property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
#property (strong ,nonatomic) NSArray *exhibitorDetailModal;
#property (strong, nonatomic) IBOutlet UILabel *LabelExhibitorDescription;
#end
Why isn't the UILabel text being populated?
Thanks.
Edit:
I have logged the 'outgoing' description of the array - [arrayOfExhibitors objectAtIndex:indexPath.row] and this shows the correct data as expected which is the same as the _exhibitorDetailModal.description which is shown in the detail view.
To get this working in the end I rewrote the entire code and tidied it up along the way.
To get the UILabel displaying properly I first passed the description property of the exhibitorDetailModal object to an NSString.
_stringExhibitorDescription = _exhibitorDetailModal.description;
I then initialised the UILabel.
self.labelExhibitorName = [[UILabel alloc]init];
Then set the text of the UILabel to be that of the NSString above
[_labelExhibitorName setText:_stringExhibitorName];
Then added the UILabel as a subView of UIView.
[self.view addSubview:_labelExhibitorDescription];

Building a Camera Application... UIScrollView will not zoom

I'm building a camera application, and I can't get the zooming on the image to work. I honestly don't know where to go from here, I'm sure it's some dumb mistake that I'm missing though. The app sets the chosenImageView to the workingImage (picture from the camera). When I try to zoom, nothing happens.
ViewController.h
#interface ViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UIScrollViewDelegate>
#property (strong, nonatomic) UIImage *workingImage;
#property (weak, nonatomic) IBOutlet UIImageView *chosenImageView;
#property (weak, nonatomic) IBOutlet UIScrollView *imageScroller;
-(IBAction)cameraButtonPressed;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageScroller.minimumZoomScale = 0.01f;
self.imageScroller.maximumZoomScale = 6.0f;
self.imageScroller.contentSize = self.imageScroller.frame.size;
self.imageScroller.scrollEnabled = YES;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.chosenImageView;
}
Here was the problem. You have to control+drag from the UIScrollView to the View Controller to set the delegate.

UIButton removeFromSuperview

I went through guide from Apple "Your first iOS app"
and now I have a button, which is not declared in ViewController:
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
- (IBAction)changeGreeting:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (copy, nonatomic) NSString *userName;
#end
Now i can remove label (and textField), using [label removeFromSuperview]; but i dont understand how to do it with button. Can someone help?
You should add an IBOutlet to the button as you did for the textfield and the label:
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *button; // Don't forget to link to this from Interface Builder
// ...
Then you can remove the button using:
[button removeFromSuperview];
Also note that the tutorial you linked to says:
The sender parameter in the action method refers to the object that is sending the action message (in this tutorial, the sender is the button).
So if you want to remove the button when it is tapped (inside changeGreeting:), then you don't need the IBOutlet because you already have a reference to the button in the sender parameter:
- (IBAction)changeGreeting:(id)sender
{
UIButton *button = (UIButton *)sender;
// ...
[button removeFromSuperview];
// ...
}
You need to declare the button in the controller like you did as an IBAction and this time declare this as an Outlet(IBoutlet).. this way you will get its reference in code..
Alternatively .. you can set a tag to the button in Interface Builder
..
and then retrieve in code using viewWithTag: method

UISlider's value is not shown by UILabel

In my app, I have a Settings view, where the user can use UISlider to set value from 50 to 1000. The selected value is then shown in a UILabel.
The problem is that the label does not display at all. The Action and the IBOutlets are connected in IB. The NSLog displays the value correctly on the console.
I can't figure out what is going on....help is needed :)
I have the following:
IBOutlet UILabel *sliderLabel;
IBOutlet UISlider *slider;
...
#property (nonatomic, retain) UILabel *sliderLabel;
#property (nonatomic, retain) UISlider *slider;
-(IBAction) sliderValueChanged:(id) sender;
and
#synthesize sliderlabel, slider;
- (IBAction) sliderValueChanged:(UISlider *) sender {
NSString *sliderValue = [NSString stringWithFormat:#"%d feet",(int)[sender value]];
NSLog(#"%#", sliderValue);
[[self sliderLabel] setText: sliderValue];
}
Thanks, and regards.

Simple slider app, quick question

Everything looks right, and yet something is missing. Could you please take a look?
.m
#synthesize textLabel;
#synthesize slider;
- (IBAction)setLabel:(id)sender {
int res = [slider value];
NSLog(#"something changed %d", res);
[textLabel setText:[NSString stringWithFormat:#"%d",[slider value]]];
}
.h
#interface SlidaAppDelegate : NSObject {
UILabel *textLabel;
UISlider *slider;
NSString *text;
}
- (IBAction)setLabel:(id)sender;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UILabel *textLabel;
#property (nonatomic, retain) IBOutlet UISlider *slider;
Whenever slider moves, i see value update correctly, however simulator shows strange numbers
Simulator shows
Xib setup is as follows:
Please advise
*UPDATE <---------------------------------------- *
Problem went away when i changed
[textLabel setText:[NSString stringWithFormat:#"%d",[slider value]]];
to
[textLabel setText:[NSString stringWithFormat:#"%d",res]];
I don't fully understand why that it though, considering that
int res = [slider value];
If possible, please explain
My bet is that value returns a float which is in between 0 and 1. So when you assign it to an int, it gets rounded down to 0.
Make res a float.
EDIT
%d wants an int, you were giving it a float with [slider value]. So it must have interpreted that value in a strange way. See DOC