How to set UILabel text using KVC in iOS ?
Suppose I have following lines of code :
#property (nonatomic, strong) UILabel *nameLabel;
[self setValue:#"someName" forKeyPath:#"nameLabel.text"];
NSLog(#"Using KVC :Output name label - %#",[self valueForKeyPath:#"nameLabel.text"]);
/* Returns nil */
#property (weak, nonatomic) IBOutlet UILabel *nameLabel;
[self setValue:#"Testing123" forKeyPath:#"nameLabel.text"];
See the image below of the storyboard
Seem like I found the fix. UILabel must be on subview, either by dragging and dropping on the storyboard and creating IBOutet or by programatically adding it on the subview.
So, #property (nonatomic, weak) IBOutlet UILabel *nameLabel; did the trick for me.
Related
Iam a newbie in iOS Development.
outlets are displayed in intellisense on .m file if i type first characters but i want to see only list of outlets in intellisense like listing only methodnames of object .
Is there a way to list only outlets in intellisense when i want?
Do i always have to switch to .h file to see the list of outlets?
.h file outlets
#property (weak, nonatomic) IBOutlet UIImageView *searchImage;
#property (weak, nonatomic) IBOutlet UIButton *searchbutton;
#property (weak, nonatomic) IBOutlet UILabel *mainLabel;
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
Use this line in .m file after implementation
#synthesize searchImage,searchbutton,mainLabel,activityIndicator;
To access them just write their name and use it
[activityIndicator stopAnimating];
mainLabel.text = #"ss";
Inside my app, I have a UIViewController which is composed of the following elements:
Two buttons and a UILabel on the top part of it
A UISCrollView which contains different elements, UILabels, Buttons and more.
I include a screenshot for you to understand the structure.
When compiled in Xcode 7 with iOS 9 SDK everything has been working properly. But now I am trying to update the app for it to work in iOS 10 and the screen does not respond when trying to scroll. The two buttons outside the UIScrollView still are working OK.
The only warning I am receiving when running on the iPhone is:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"",
""
)
IB also tells me that there is a mistake with the constraints (red indicator). It tells that ScrollVertical (UISCrollView) need constraints for: Y position or Height. But I'm quite sure that this message was shown in xcode7-ios9, when everything worked properly.
Any idea what is happening? Any idea would be appreciated, since I have been struggling with this for a day.
This arre the constraints expanded:
Thanks a lot
Maybe you should expend the Constraints node in your view hierarchy, and capture the constraints your have defined in Xcode, and paste it here. That would be helped for issue tracking.
Same problem here, but with a twist. On first startup of subscreen scrolling won't work. After rotating the device it works fine. It makes no difference if it starts on portrait or landscape oritation. after a rotating the app works fine. No problems with IOS9 and IOS8.
#import <QuartzCore/QuartzCore.h>
#import "ApDetailTableViewController.h"
#import "ApMailViewController.h"
#import "ApApplicatieData.h"
#import "ztatz_constants.h"
#import "ztatz_macro.h"
#interface ApDetailTableViewController ()
#property (weak, nonatomic) IBOutlet UIView *contentUIView;
#property (weak, nonatomic) IBOutlet UIScrollView *apUIScrolView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewHeightConstraint;
#property (weak, nonatomic) IBOutlet UIView *lastSeperatorView;
#property (weak, nonatomic) IBOutlet UILabel *applicatienaam;
#property (weak, nonatomic) IBOutlet UILabel *san;
#property (weak, nonatomic) IBOutlet UILabel *beschrijving;
#property (weak, nonatomic) IBOutlet UILabel *status;
#property (weak, nonatomic) IBOutlet UILabel *updatetime;
#property (weak, nonatomic) IBOutlet UILabel *afdelingsnaam;
#property (weak, nonatomic) IBOutlet UILabel *contactpersoon;
#property (weak, nonatomic) IBOutlet UILabel *email;
#property (weak, nonatomic) IBOutlet UILabel *controledatum;
#end
#implementation ApDetailTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.applicatienaam.text = self.apDataForDetails.applicatienaam;
self.san.text = self.apDataForDetails.san;
self.beschrijving.text = self.apDataForDetails.beschrijving;
self.status.text = self.apDataForDetails.status;
self.updatetime.text = self.apDataForDetails.updatetime;
self.afdelingsnaam.text = self.apDataForDetails.afdelingsnaam;
self.contactpersoon.text=self.apDataForDetails.contactpersoon;
self.email.text=self.apDataForDetails.email;
self.controledatum.text=self.apDataForDetails.controledatum;
// start a top of scroll view
self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// limit the slack space at the end of the scroll view.
DLog(#"y = %f\n", self.lastSeperatorView.frame.origin.y);
self.contentViewHeightConstraint.constant = self.lastSeperatorView.frame.origin.y+4;
}
#end
I have a UIView with two UILabel that I want to reuse in more than one UIViewController. I use storyboard and I assign a custom class alertView:UIView that I declared.
file AlertRemote.h
#interface AlertRemote: UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
-(void) setTextLabel;
file AlertRemote.m
-(void) setTextLabel{
[ _label1 setText:#"attention..."];
[_label2 setText:#"the program..."];
}
file Controllo.m
//the view present in the storyboard alertView linked to the uiview
#property (strong, nonatomic)IBOutlet AlertRemote *alertView;
#property AlertRemote *alRemView;
[super viewDidLoad];
_alertView=_alRemView; [_alertView setTextLabel];
[_alertView setTextLabel];
if I put some breakpoints inside setTextLabel the code don't works
thanks!!
In order for the custom alert to work, you need to initialise it.
AlertRemote *alRemView;
alRemView = [[AlertRemote alloc]init];
[alRemView setTextLabel];
I think you are not initializing the property alertview. If you are initializing the property alertview then try to set directly with out using alremView.
I have a custom view - DetailSubView.h with labels
#property (retain, nonatomic) UILabel *titleLabel;
#property (retain, nonatomic) UILabel *descHeaderLabel;
On custom UICollectionViewCell, i have
#property (strong, nonatomic) DetailSubView *detailView;
When I set text, i received unrecognized selector error.
[self.detailView.titleLabel setText:#"text"];
That's because your titleLabel is declared in DetailView but you're trying to access it from a class of type DetailSubView. It cannot access the getter method for your property on DetailSubView since you don't have it declared there.
I have created one segmented control and a text view in my .xib file and I have declare it in .h file as
#interface controlsViewController : UIViewController {
IBOutlet UISegmentedControl *colorChooser;
IBOutlet UITextView *setText;
}
#property (nonatomic,retain) UISegmentedControl *colorChooser;
#property (nonatomic,retain) UITextView *setText
but it shows me warning on both lines of #property
Can anyone tell me why it warns me?
My guess would be that you don't have a #synthesize or #dynamic in your implementation. That would generate compiler warnings.
The position of the IBOutlet wound not generate a compiler warning as it's a marco of nothing. It's used by xcode resource editor (or the older Interface Builder) to indicate it's a outlet property and doesn't generate any code.
#interface controlsViewController : UIViewController {
UISegmentedControl *colorChooser;
UITextView *setText;
}
#property (nonatomic,retain) IBOutlet UISegmentedControl *colorChooser;
#property (nonatomic,retain) IBOutlet UITextView *setText;
You need to synthesize the property in your .m file. You won't get the error then.