I have created a text label (title_label) in Interface Builder, I have declared it in my FirstViewController.h file and I would now like to add a border to it. I have added the code to do this but when I run the app the border simply doesn't appear.
Here is the code:
#import "FirstViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
title_label.layer.borderColor = [UIColor greenColor].CGColor;
title_label.layer.borderWidth = 4.0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This is the content of FirstViewController.h:
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController {
IBOutlet UILabel *title_label;
}
#end
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *title_label = [[UILabel alloc]initWithFrame:CGRectMake(20, 30, 150, 40)];
title_label.text = #"Text Which Comes";
title_label.layer.borderColor = [UIColor greenColor].CGColor;
title_label.layer.borderWidth = 4.0;
title_label.layer.cornerRadius = 5.0;
[self.view addSubview:title_label];
}
Imported QuartzCore framework
I have tried with your same code, for me its working fine. I think you may forget
1.declare with IBOutlet and
2.connect with xib label.
Check once again
Related
I am using UISearchBar for search purposes using Objective C. I need the placeholder text of search bar to be aligned to the center of the search bar programatically and when user starts typing, text must be aligned left.
Thanks in Advance
I had a same problem like yours before. I couldn't find any workaround and after all I just used UILabel and UISearchBarDelegate methods.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate>
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController () {
UISearchBar *srchBar;
UILabel *customPlaceHolderForSearchBar;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frm = CGRectMake(0, 0, self.view.frame.size.width, 80);
srchBar = [[UISearchBar alloc] initWithFrame:frm];
srchBar.delegate = self;
[self.view addSubview:srchBar];
customPlaceHolderForSearchBar = [[UILabel alloc] initWithFrame:frm];
customPlaceHolderForSearchBar.text = #"PlaceHolder text";
customPlaceHolderForSearchBar.textColor = [UIColor grayColor];
customPlaceHolderForSearchBar.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:customPlaceHolderForSearchBar];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
customPlaceHolderForSearchBar.hidden = YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
if (searchBar.text.length < 1) {
customPlaceHolderForSearchBar.hidden = NO;
}
}
#end
I'm writing an iOS Today Extension that has some UIImageViews in it. I want to set images from an url in them and so I thought using SDWebImage would be best. I wrote the code below:
#import "TodayViewController.h"
#import <NotificationCenter/NotificationCenter.h>
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.m"
#import "SDImageCache.m"
#interface TodayViewController () <NCWidgetProviding>
#property (strong, nonatomic) UIImageView *firstImage;
#property (strong, nonatomic) UILabel *titleLabel;
#property (strong, nonatomic) NSDictionary *dataOne;
#end
#implementation TodayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self updateData];
self.preferredContentSize = CGSizeMake(self.view.frame.size.width, 230);
NSInteger quarterSize = self.view.frame.size.width/4;
NSInteger eightSize = quarterSize/4;
self.firstImage = [[UIImageView alloc] initWithFrame:CGRectMake(eightSize, 45, quarterSize, quarterSize*1.25)];
[self.firstImage sd_setImageWithURL:[NSURL URLWithString:#"http://anluan.com/crest2.jpg"]];
[self.view addSubview:self.firstImage];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(eightSize, self.firstArticle.frame.origin.y + self.firstArticle.frame.size.height + 10, quarterSize, 20)];
self.titleLabel.text = [self.dataOne objectForKey:#"title"];
self.titleLabel.numberOfLines = 2;
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:13];
[self.titleLabel sizeToFit];
[self.view addSubview:self.titleLabel];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(userDefaultsDidChange:)
name:NSUserDefaultsDidChangeNotification
object:nil];
}
return self;
}
- (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:(UIEdgeInsets)defaultMarginInsets
{
return UIEdgeInsetsZero;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData
completionHandler(NCUpdateResultNewData);
}
- (void)userDefaultsDidChange:(NSNotification *)notification {
[self updateNumberLabelText];
}
- (void)updateNumberLabelText {
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:#"group.company.TodayExtensionDefaults"];
self.dataOne = [defaults objectForKey:#"dataOne"];
}
}
#end
However, this keeps crashing, throwing this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView sd_setImageWithURL:]: unrecognized selector sent to instance 0x7a88cc30'
First, remove following:
#import "UIImageView+WebCache.m"
#import "SDImageCache.m"
You never have to import implementation lines, only headers. In general principle of OOP you should hide implementation from other classes, thats called encapsulation.
Second, import #import <SDWebImage/UIImageView+WebCache.h>
That file have declaration of your setImageWithUrl method.
Cheers.
I have recently started learning core graphics to draw on views, but every time I call setNeedsDisplay, the method runs but doesn't trigger drawRect. In my project, I have a view on a view controller. The view contains an if statement in drawRect that checks if a BOOL variable is YES or NO. If YES, it uses the code to draw a red circle. If NO, it draws a blue circle. In the BOOL's setter, it calls setNeedsDisplay. I create an instance of this view class in the ViewController and set the BOOL but the view is not redrawing. Why is the method not getting called? I posted the xcode files and code bits below.
Customview.m:
#import "CustomView.h"
#implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
-(void)awakeFromNib{
[self setup];
}
-(void)setup{
_choice1 = YES;
}
-(void)setChoice1:(BOOL)choice1{
_choice1 = choice1;
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
if (_choice1) {
UIBezierPath * redCircle = [UIBezierPath bezierPathWithOvalInRect:rect];
[[UIColor redColor]setFill];
[redCircle fill];
}else if (!_choice1){
UIBezierPath * blueCircle = [UIBezierPath bezierPathWithOvalInRect:rect];
[[UIColor blueColor]setFill];
[blueCircle fill];
}
}
#end
CustomView.h:
#import <UIKit/UIKit.h>
#interface CustomView : UIView
#property (nonatomic) BOOL choice1;
#end
ViewController.m:
#import "ViewController.h"
#import "CustomView.h"
#interface ViewController ()
- (IBAction)Change:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Change:(id)sender {
CustomView * cv = [[CustomView alloc]init];
[cv setChoice1:NO];
}
#end
Full project: https://www.mediafire.com/?2c8e5uay00wci0c
Thanks
You need to create an outlet for your CustomView in your ViewController. Delete the code that creates the new CustomView instance and use _cv to refer to the customView.
#interface ViewController ()
- (IBAction)Change:(id)sender;
#property (weak,nonatomic) IBOutlet CustomView *cv;
#end
Then, in the storyboard, connect ViewController's cv outlet to the CustomView. Also, you'll need to implement the initFromCoder: method on CustomView.
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self setup];
}
return self;
}
Okay so i started learning UIDynamics and wanted to just work it out on small box.
I declared UIGravityBehaviour and UIDynamicsBehaviour in my ViewController.m and coded the UIView, but when i executed the code, it was not working.
But just as i declared them as #property in my .h file, the program was working completely fine.
What is the reason behind that?
What is the difference between declaring it as #property and without declaring it as #property?
Here are my files.
Before using #property
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
UIDynamicAnimator* myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior* gravityBehaviour = [[UIGravityBehavior alloc] initWithItems:#[box]];
[myAnimator addBehavior: gravityBehaviour];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
After Declaring it as #property
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong,nonatomic) UIDynamicAnimator* myAnimator ;
#property (strong,nonatomic) UIGravityBehavior *gravityBehavior;
#property (strong,nonatomic) UICollisionBehavior *collisionBehavior ;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
self.myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[box]];
self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[box]];
self.collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.myAnimator addBehavior:self.gravityBehavior];
[self.myAnimator addBehavior:self.collisionBehavior];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You don't have to move UIGravityBehavior to a property, only UIDynamicAnimator to make it work. However, it's probably a good idea anyway in case you want to remove specific gravity behaviours rather than all of them at once.
If you are using ARC:
The UIDynamicAnimator probably does not have a strong reference to the UIGravityBehavior. So after your viewDidLoad method returns, no one owns that UIGravityBehavior and ARC will make sure that that point it is released. When you make a property for it, your viewController owns it, so the UIGravityBehavior will survive until you viewController gets deallocated.
I have this .h code :
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *fieldEmail;
#property (strong, nonatomic) IBOutlet UITextField *fieldPassword;
#property (strong, nonatomic) IBOutlet UILabel *titleLogin;
- (IBAction)buttonRegister;
- (IBAction)buttonLogin;
- (IBAction)loginFacebook;
- (IBAction)loginTwitter;
#end
and this is my .m code :
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[_fieldEmail setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
[_titleLogin setFont:[UIFont fontWithName:#"Raleway-ExtraLight" size:28]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonRegister {
}
- (IBAction)buttonLogin {
}
- (IBAction)loginFacebook {
}
- (IBAction)loginTwitter {
}
#end
now, what I want to do is to add padding on left and right of these 2 text fields : fieldEmail and fieldPassword so it has some space because I put an image as background on both text fields.
I tried to add this code on my .m file under - (void)viewDidLoad :
UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[UITextField setLeftViewMode:UITextFieldViewModeAlways];
[UITextField setLeftView:fieldEmail];
but my XCode gave me this error message :
no known class method for selector 'setLeftViewMode'
no known class method for selector 'setLeftView'
by the way, I'm using XCode 4.6.3. does this version have special code to add padding on text field? thank you.
leftViewMode and leftView are properties of UITextField, not class methods. You need to assign them to instances.
yourTextField.leftViewMode = UITextFieldViewModeAlways;
yourTextField.leftView = fieldEmail;