UILabel not updating even though it is connected to IBOutlet and IBOutlet has correct value - uilabel

I have a TableViewCOntroller that shows a list of images and associated descriptions. I pass data to the detailed view in the prepare for segue method. THe MSMutableDrictionary Object I pass has the correct values and I can see this in the NSLog of the object in the detail views viewDidLoadMethod. THe code for this is as follows:
.h of detail view controller
#interface sitespecLargeImageDetail : UIViewController
#property (strong, nonatomic) NSMutableDictionary *details;
#property (strong, nonatomic) IBOutlet UIImage *largeImage;
#property (strong, nonatomic) IBOutlet UILabel *largeImageLabel;
#end
.m of detail view controller
#implementation sitespecLargeImageDetail
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_largeImage = [_details valueForKey:#"pbvurl"];
_largeImageLabel = [_details valueForKey:#"pbvdesc"];
NSLog(#"%#", _largeImageLabel);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the prepare for segue event in the tableviewcontoller:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
sitespecLargeImageDetail *detailViewController = (sitespecLargeImageDetail *)segue.destinationViewController;
detailViewController.details = _dictObj;
}
Couple of strange things:
WHen I look at the detail view controller in my storyboard in the connections inspector, I can see both the label and image Outlets. I can see I ahve connected the label outlet. great, but it does not update when the view loads! I know the dict obj in the detail view gets data because I can NSLog it and see it. I use that to set the IBOutlet for the label but nothing.
In the connection inspector, I can see the UIImage outlet BUT I am unable to connect it to the UIImage VIew I placed in the detail view controller, it simply wont allow me to connect it, no error, just when I control drag from the detailview controller I dont even see it! I see the label outlet, but not the image view.......

Firstly, what you want to do is _largeImageLabel.text = [_details valueForKey:#"pbvdesc"];.
Without the .text what you are doing is using the UILabel pointer to point to a NSString, which clearly does not result in your expected behaviour!
As for the UIImage outlet, what you need is a UIImageView outlet! UIImage is not a view, hence it doesn't have any relation with the interface builder (you can't drag a UIImage in your storyboard, only views can compose interfaces).

Related

Obj-C / Using MapKitView and TableView together with delegation

I am a very beginner of objective c and I wanted to create a scene about using 2 child view controller in a master view controller which includes MapKit and TableView. I ve searched it on internet for 1 day and but I couldn't make any of solutions since I failed in different steps of each suggestions. I know that there are a lot of way to pass data between View Controllers and I thought the best way is using delegation logic in this situation. (let me know if I am wrong please). By the way, I can update or move the cursor onto specific location WHEN I set a dummy button on MapKitViewController, so MapKit part is not where I am failing at. I am pretty sure that the problem is about communication between 2 View Controllers which are active at the same time.
Problem:
Updating MapView by clicking a table row of TableView which includes location coordinate detail. Both children view are set on a MasterViewController by 2 container views.
Tried so far:
Created a method "goToLocation:(Location*) location" in MapViewController and sent parameters to this method from TableViewController. =>(Lat and Long parameters received by goToLocation() but MapKitView doesn't get updated)
Tried to create a delegate logic between MapView and TableView.=>(Probably I couldn't create it properly. Please see below)
What I want :
I want to know what part I am doing wrong. Is there a easy or proper way to achieve that ?
TableViewController.h
#class TableViewController; //define class
#protocol TableViewDelegate <NSObject> //define delegate protocol
//define delegate method to be implemented within another class
- (void) locationSelected: (TableViewController *) sender object:(Location *) location;
#end
#property (nonatomic, weak) id <TableViewDelegate> delegate; //define TableViewVCDelegate as delegate
TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self notifyNow:[_locations objectAtIndex:indexPath.row]];
}
- (void) notifyNow:(Location *) location {
//this will call the method implemented in your other class
[self.delegate locationSelected:self object:(Location *) location];
NSLog(#"Selected: %#",[location name]);
}
MapViewController.h
//make it a delegate for TableViewVCDelegate
#interface MapViewController : UIViewController <TableViewDelegate>
MapViewController.m
- (void)viewDidLoad {
_tableViewController = [[TableViewController alloc]init];
_tableViewController.delegate = self; //set its delegate to self somewhere
}
!!_ This Method Doesn't Get Triggered _!!
- (void)locationSelected:(TableViewController *)sender object:(Location *)location {
NSLog(#"%# is great!", [location name]);
}
MainViewController.m (Master/Root View Controller)
#import "MainViewController.h"
#import "MapViewController.h"
#import "TableViewController.h"
#interface MainViewController ()
#property (strong, nonatomic) IBOutlet UIView *topCont;
#property (strong, nonatomic) IBOutlet UIView *bottomCont;
#property (strong, nonatomic) IBOutlet UIView *safeArea;
#end
#implementation MainViewController
MapViewController *mapViewVC;
TableViewController *tableViewVC;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES animated:YES];
tableViewVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableViewController"];
mapViewVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TopChildVC"];
[self addChildViewController:tableViewVC];
[tableViewVC.view setFrame:CGRectMake(0.0f,
0.0f,
self.safeArea.frame.size.width,
self.safeArea.frame.size.height/2)];
[self.bottomCont addSubview:tableViewVC.view];
[tableViewVC didMoveToParentViewController:self];
[self addChildViewController:mapViewVC];
[mapViewVC.view setFrame:CGRectMake(0.0f,
0.0f,
self.safeArea.frame.size.width,
self.safeArea.frame.size.height/2)];
[self.topCont addSubview:mapViewVC.view];
[mapViewVC didMoveToParentViewController:self];
}
#end

How can I send selectedSegmentIndex to containerView in Objective-c?

I have a containerView into my viewController. My viewController has a UISegmentedControl.
How can I send this selectedSegmentIndex selected at this moment and everytime when I change this?
I', trying with a property in the next class with prepareForSegue but this only send on first load..
Thanks!
Then Edit I'm getting: -[ViewController containerViewDidChangeSegmentIndex:]: unrecognized selector sent to instance 0x7f8db16286d0
FirstViewController (This contains A select and a containerView)
ViewController.h
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISegmentedControl *select;
#property (weak, nonatomic) IBOutlet UIView *container;
#property(nonatomic, assign) ContainerViewController * classLevelReference;
#end
ViewController.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_select addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentChanged:(UISegmentedControl *)segment{
//since you've reference to your container view here, you can directly call its method here:
[self.classLevelReference containerViewDidChangeSegmentIndex:segment.selectedSegmentIndex];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//don't forget to check the segue id, if you've multiple segues
ContainerViewController *containerView = [segue destinationViewController];
self.classLevelReference = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ContainerViewController.h
#interface ContainerViewController : UIViewController
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex;
#end
ContainerView.m
#implementation ContainerViewController
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex{
//Do whatever you want with your updated index
NSLog(#"changing");
}
#end
You might want to make a method in your ContainerViewController.h like this:
#interface ContainerViewController:UIViewController
//.....other implementation here.
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex;
#end
Now implement this method in your ContainerViewController.m like this:
- (void)containerViewDidChangeSegmentIndex:(NSInteger)updatedIndex{
//Do whatever you want with your updated index
}
Now in your prepare for segue, save the reference to your ContainerViewController in a class-level variable :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//don't forget to check the segue id, if you've multiple segues
ContainerViewController *containerView = [segue destinationViewController];
self.classLevelReference = containerView;
}
Lastly in the FirstViewController.m, tell the container view when segment index changes.
- (void)viewDidLoad{
//.....your other implementation here.....
//add a listener to your segment's value changed action
[youregements addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentChanged:(UISegmentedControl *)segment{
//since you've reference to your container view here, you can directly call its method here:
[self.classLevelReference containerViewDidChangeSegmentIndex:segment.selectedSegmentIndex];
}
Important: you might've different names for the ContainerViewController and FirstViewController in your case, just apply the changes carefully.
Happy coding!

Accessing View from ViewController

I created a ViewController in StoryBoard and linked it with the GamePanelViewController class.
Furthermore I linked the view of the ViewController with a second class called GamePanel which extends of UIView.
How can I access to the view which is created automatically by StoryBoard to perform some methods I implemented to the GamePanel class which is linked to the view by identity inspector?
GamePanel.h:
#import <UIKit/UIKit.h>
#import "Plattform.h"
#interface GamePanel : UIView
#property (strong, nonatomic) NSMutableArray *plattforms;
- (void)initGamePanel;
- (void)drawPlattforms:(CGContextRef)gc;
#end
GamePanel.m:
#import "GamePanel.h"
#implementation GamePanel
#synthesize plattforms;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)initGamePanel{
self.backgroundColor = [UIColor redColor];
self.plattforms = [NSMutableArray new];
// Initialization code
for(int i = 0;i<8;i++){
for(int j = 0;j<6;j++){
[self.plattforms addObject:[[Plattform alloc] initWithXCord:(14+j*37) andYCoord:(58+14+i*37)]];
NSLog(#"Init");
}
}
}
If you have actually linked the viewController's view property with your custom view, then you can simply use self.view within you viewController to access it.
If you have just dragged a UIView onto your viewController in your storyboard (and not linked it to the viewController's view property), you will need to create an IBOutlet for it (control-drag from the view to your interface, and give it a name), then you can reference it with the name you give it e.g. self.myView.

changing UILabel text on a subview from main view

Ok, so I'm a relative noob with Objective-C/iOS programming, so hopefully someone with more knowledge here can help me out.
I have an iPad application using the SplitViewController template (with Core Data). I created another UIViewController (with xib file) called PlayerViewController. This View has several UILabel components on it.
I have a list of players that show up in the RootViewController (UITableView) and when you select a player, I programmatically create a PlayerViewController (in DetailViewController), pass it the NSManagedObject that was passed to the DetailViewController, try to set the text of one of the labels on the PlayerViewController's view, and then add it as a subview to the DetailViewController.
All of this works great except for the setting the text of the label on the PlayerViewController's view. I'm not sure what I'm doing wrong. I have used NSLog to confirm that the NSManagedObject is not nil and that the NSManagedObject property I'm trying to use has the correct text.
I'm at a loss here. Any help would be greatly appreciated. (Code follows):
This method is in the DetailViewController.m file:
- (void)configureView {
// Update the user interface for the detail item.
PlayerViewController *player = [[PlayerViewController alloc] init];
player.player = detailItem;
[self.view addSubview:player.view];
}
This method is called when the user selects an item from the RootViewController (This functionality, calling of configureView, is setup by the template and I haven't changed it).
Setting the player property of the PlayerViewController to object detailItem is handled in the setPlayer method of that class.
- (void)setPlayer:(NSManagedObject *)managedObject {
if (player != managedObject) {
[player release];
player = [managedObject retain];
// Update the view.
[self configureView];
}
}
I then have a configureView method as well in PlayerViewController that sets the text of the label:
- (void)configureView {
nickName.text = [[player valueForKey:#"Nickname"] description];
NSLog(#"Nickname %#", [[player valueForKey:#"Nickname"] description]);
NSLog(#"Nickname %#", nickName.text);
}
Ok, so the first NSLog statement prints the desired value, but the text of the UILabel (called nickName) returns nil.
The following is the full PlayerViewController.h & .m files:
PlayerViewController.h:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#interface PlayerViewController : UIViewController {
NSManagedObject *player;
IBOutlet UILabel *nickName;
IBOutlet UILabel *goalCount;
IBOutlet UILabel *assistCount;
IBOutlet UILabel *timeInGame;
}
#property (nonatomic, retain) IBOutlet UILabel *nickName;
#property (nonatomic, retain) IBOutlet UILabel *goalCount;
#property (nonatomic, retain) IBOutlet UILabel *assistCount;
#property (nonatomic, retain) IBOutlet UILabel *timeInGame;
#property (nonatomic, retain) NSManagedObject *player;
#end
PlayerViewController.m:
#import "PlayerViewController.h"
#implementation PlayerViewController
#synthesize nickName, goalCount, assistCount, timeInGame, player;
#pragma mark -
#pragma mark Managing the detail item
/*
When setting the player item, update the view
*/
- (void)setPlayer:(NSManagedObject *)managedObject {
if (player != managedObject) {
[player release];
player = [managedObject retain];
// Update the view.
[self configureView];
}
}
- (void)configureView {
nickName.text = [[player valueForKey:#"Nickname"] description];
NSLog(#"Nickname %#", [[player valueForKey:#"Nickname"] description]);
NSLog(#"Nickname %#", nickName.text);
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I'm sure I'm just missing something trivial, but I can't figure it out, and haven't been able to find any answers searching the web.
Thanks for any help!
Ok, so after playing with this for a bit and searching and searching around, I have gotten the answer to my problem. It turns out all the code I had was fine except the location of one statement. My call to configureView in PlayerViewController.m needed to be in viewDidLoad() not in the setPlayer() method. It all works great now.
Change the configureView method to that :
- (void)configureView {
nickName.text = (NSString*)[player valueForKey:#"Nickname"];
}
Yes, better place to call method is
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self configureView];
}
(void)setPlayer:(NSManagedObject *)managedObject called before your nib files loaded.

Why isn't my UIButton responding to touches?

I'm sure I'm overlooking the obvious as I've got countless working buttons...but...for whatever reason this one is not cooperating...
I've added a UIButton (Rounded Rect) to a UIView subclass (DialogView) which is a subview of my view controller's view. This subview is created almost entirely in IB. I've wired up the button to (IBAction)okButtonPressed:(id)sender in IB to Touch Up Inside and created a corresponding method in DialogView. However when I "touch" this button it doesn't trigger the method. userInteractionEnabled is true for the VC's view, DialogView and the UIButton.
Thinking maybe initWithCoder had to do some frame manipulation or something I added the following which successfully logs to console.
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSLog(#"DialogView initWithCoder called");
}
return self;
}
In further exploration I wired up an IBOutlet to the button and then if I try to change the titleLabel from the view controller I notice that it get's severely truncated. Default text of say "Press Me!" set in IB displays fine when view is first drawn. But if I change the text...
self.DialogView.okButton.titleLabel.text = #"Not Working";
...it gets truncated to "N..."
Dunno if this is related. Probably...
Anyone see what I've screwed up here?
Edit (adding code related to showing UIButton):
From the View Controller:
self.DialogView = [[[NSBundle mainBundle] loadNibNamed:#"DialogView" owner:self options:nil] objectAtIndex:0];;
self.DialogView.myVC = self;
self.DialogView.backgroundColor = [UIColor clearColor];
self.DialogView.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
self.DialogView.nameLabel.text = loan.fullName;
self.DialogView.noteLabel.text = loan.summaryOfLoan;
self.DialogView.amountLabel.text = [currencyFormatter stringFromNumber:loan.originalAmount];
self.DialogView.alpha = 0.0;
[self.view addSubview:DialogView];
The UILabels all displaying as expected. As is the problem UIButton. I can see it I just can't interact with it!?!
DialogView's interface:
#class MyViewController;
#interface DialogView : UIView {
IBOutlet UILabel *nameLabel, *noteLabel, *amountLabel;
IBOutlet UIImageView *arrowView;
IBOutlet UIButton *okButton;
MyViewController *myVC;
}
#property (nonatomic, retain) UILabel *nameLabel, *noteLabel, *amountLabel;
#property (nonatomic, retain) UIImageView *arrowView;
#property (nonatomic, assign) MyViewController *myVC;
#property (nonatomic, retain) UIButton *okButton;
- (IBAction)okButtonPressed:(id)sender;
#end
And DialogView's implementation:
#import "DialogView.h"
#import "MyViewController.h"
#implementation DialogView
#synthesize nameLabel, noteLabel, amountLabel, arrowView, okButton;
#synthesize myVC;
- (void)dealloc {
[nameLabel release];
[noteLabel release];
[amountLabel release];
[arrowView release];
[okButton release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
NSLog(#"DialogView initWithCoder called");
}
return self;
}
- (IBAction)okButtonPressed:(id)sender {
NSLog(#"pressed DialogView OK button");
[self.myVC.navigationController popViewControllerAnimated:YES];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
#end
I thought that we should use -setTitle:forState: in order to set button's title ?
An other thought, did you check that the button's frame is not CGRectZero ? And by the way, all the frames for the view in the hierarchy ? And check that one superview in the hierarchy is not user interaction disabled ?
And, I think imageView does not respond to touches, do you have one in your code ?
I was just having more or less the same problem and I found that my containing view did not have "User Interaction Enabled".
Hope this helps.
Do you maybe have two buttons on top of one another? Change the IB project window to the detail view and see if your view has more buttons than you are expecting. Maybe you've wired up a button that's not actually getting the press you're expecting.