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];
Related
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.
I use a UITableViewController to display the details of KoreanFood. The first cell is a custom UITableViewCell (OverviewCell) with an Image and two UITextFields, which I created and layout in Storyboard (AutoLayout).
I subclassed UITableviewCell like this:
// OverviewCell.h
#interface OverviewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UITextField *englishTitleTF;
#property (strong, nonatomic) IBOutlet UITextField *koreanTitleTF;
#property (strong, nonatomic) IBOutlet UIImageView *myImageView;
#property (strong, nonatomic) UIImage *thumbnail;
My textfields in Storyboard are set to enabled/UserInteractionenabled and the delegate is my TVC. When I create the cells I also do this in code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == GENERAL_SECTION) {
static NSString *overviewCellID = #"overviewCell";
OverviewCell *overviewCell = [tableView dequeueReusableCellWithIdentifier:overviewCellID forIndexPath:indexPath];
overviewCell.englishTitleTF.text = self.generalInfo.titleEnglish;
overviewCell.koreanTitleTF.text = self.generalInfo.titleKorean;
overviewCell.englishTitleTF.enabled = YES;
overviewCell.koreanTitleTF.enabled = NO;
//BOOL test = [overviewCell.englishTitleTF becomeFirstResponder];
overviewCell.koreanTitleTF.delegate = self;
overviewCell.englishTitleTF.delegate = self;
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.generalInfo.thumbnail];
overviewCell.myImageView = imageView;
overviewCell.myImageView.frame = CGRectMake(25, 25, 95, 95);
[overviewCell addSubview:overviewCell.myImageView];
return overviewCell;
}
The comment with the BOOL is NO, and I just don't know why... As I set the text and it's displayed correctly, I know the Outlets are set and the Cell isn't nil (I checked that).
Why does this not become first responder?
I also tried some suggestions inside the OverviewCell subclass like the hit test or implementing the setSelected: / setEditing: methods. But a becomeFirstResponder to the textField here doesn't change anything as well.
A view can't become first responder until it's been added to the responder chain, which happens automatically when the view gets added as a subview of a view that's in a window in the application object's windows list. In other words, your cell hasn't been added to the table view yet, so it's not connected to anything, and hence can't become first responder.
Try sending becomeFirstResponder from a method that gets called after the table view has finished loading its cells. And of course, don't do this:
overviewCell.koreanTitleTF.enabled = NO;
I have two location labels on my first view controller I need them to be displayed on another view controller when opened, what is the best way to implement this?
The labels i want to pass look like this:
latitudeLabel.text = [NSString stringWithFormat:#"latitude=%f", location.coordinate.latitude];
longitudeLabel.text = [NSString stringWithFormat:#"longitude=%f", location.coordinate.longitude];
i had created a sample for sharing data between classes.. All you need to do is explained here
Before you push secondViewController you can add your labels as subviews of second ViewController's view
SecondViewController *cntr = [[SecondViewController alloc] initWithNibName:#"" bundle:nil];
[cntr.view addSubview latitudeLabel];
[cntr.view addSubview longitudeLabel];
[self.navigationController pushViewController:cntr animated:YES]
You have to create identical labels on both view controllers and pass only the string data. You can pass parameters from one class to another with properties (since they are public).
In first view controller .m:
- (void)openSecondViewController {
SecondViewController *controller = [SecondViewController alloc] init];
controller.data = [NSArray arrayWithObjects:#"First String", #"Second String", nil];
[self.navigationController pushViewController:controller];
}
In second view controller .h:
#interface SecondViewController : UIViewController
#property (weak, nonatomic) id data;
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#end
In second view controller .m:
- (void)setData:(id)data {
self.label1.text = [data objectAtIndex:0];
self.label2.text = [data objectAtIndex:1];
}
I have an UITableVIew. Now I made my own UITableViewCell. But if my table appears nothing is shown. However the cells are still selectable and open the DetailView. Which command have I forgotten?
//Customize the appearance of table view cells.
-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MainTableCell";
MainTableCell *cell = (MainTableCell*)[tableView dequeueReusableCellWithIdentifier:Cel lIdentifier];
if (cell == nil) {
cell = [[[MainTableCell alloc] initWithStyle:UITableViewCellStyleDef ault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
VerwaltungInformation *selectedFormel = [listOfFormularies objectAtIndex:indexPath.row];
cell.FormelNameLabel.text = selectedFormel.nameFormel;
return cell;
}
Do I have to add special things? If somebody needs more code - please tell me.
Here is my MainTableCell.h:
import <UIKit/UIKit.h>
#interface MainTableCell : UITableViewCell {
UILabel *FormelNameLabel;
UILabel *ErklaerungVerfuegbarLabel;
UILabel *BeispielVerfuegbarLabel;
UILabel *RechnerVerfuegbarLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *FormelNameLabel;
#property (nonatomic, retain) IBOutlet UILabel *ErklaerungVerfuegbarLabel;
#property (nonatomic, retain) IBOutlet UILabel *BeispielVerfuegbarLabel;
#property (nonatomic, retain) IBOutlet UILabel *RechnerVerfuegbarLabel;`
#end
And here is my MainTableCell.m:
#import "MainTableCell.h"
#implementation MainTableCell
#synthesize FormelNameLabel;`
#synthesize ErklaerungVerfuegbarLabel;
#synthesize BeispielVerfuegbarLabel;
#synthesize RechnerVerfuegbarLabel;`
- (void)dealloc {
[FormelNameLabel release];
[ErklaerungVerfuegbarLabel release];
[BeispielVerfuegbarLabel release];
[RechnerVerfuegbarLabel release];
[super dealloc];
}
#end
you have commented this line
cell. VerwaltungInformation *selectedFormel = [listOfFormularies objectAtIndex:indexPath.row];
and also check wthr
selectedFormel.nameFormel
contains any value.
ok, I'll try to answer.
1.try to NSLog this value selectedFormel.nameFormel before setting it to the label.text.
NSLog(#"selectedFormel.nameFormel:%#", selectedFormel.nameFormel);
2.Have you configured your cell properly? are you sure that for example FormelNameLabel's frame is not zero?
NSLog(#"cellFrame:%#", NSStringFromCGRect(cell.frame));
Are you sure that you have added a label to the cell as subview?
If all these things are done and it still shows nothing provide a code from your cell!
I've decleared a UILabel in my view controller
#interface ViewController : UIViewController
{
UILabel *statusText;
}
#property (nonatomic,retain) IBOutlet UILabel *statusText;
-(IBAction)buttonPressed:(id)sender;
#end
Now I want the UILabel's title to change when my button is pressed, but my statusText can not set the properly. for example:
- (IBAction)buttonPressed:(id)sender{
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc]initWithFormat:#"%#button pressed",title];
statusText.text = #"helloworld";
NSLog(#"%#",statusText.text);
[newText release];
}
the log returns
2012-01-25 00:46:27.663 buttonfun[34247:f803] (null)
It means that the text property "helloworld" has not been set. Why?
Try logging this instead:
NSLog(#"%#",statusText);
If it's also null, it means you haven't hooked up your statusText IBOutlet to your UILabel in Interface Builder (you need to ctrl-drag from your File's Owner to the label and then select statusText).