I'm following a tutorial from Lynda regarding creating a UIDatePicker, and here's what my code looks like:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
...
#end
ViewController.m
#import "ViewController.h"
...
#implementation ViewController
...
- (IBAction)displayDate:(id)sender {
NSDate *chosen = [datePicker date];
...
}
#end
but for some reason XCode is giving me an error:
"unknown receiver 'datePicker'
Just a note, the datePicker is linked. Is there something that I'm missing?
Forget to #syntesize
Your UIPickerView to your .m file
and also your can access by UIPickerView by name _datePicker
In this case you have datePicker property which is accessible by self.datePicker, or instance variable (generated for you by compiler) _datePicker. You really want the former.
When using properties, you must call self first:
NSDate *chosen = [self.datePicker date];
Or
NSDate *chosen = [[self datePicker] date];
Related
I have below.
#interface MyViewController () {
NSDate *myCurrentDate;
}
#implementation MyViewController
-(void)viewDidLoad {
[super viewDidLoad];
myCurrentDate = [NSDate date];
}
- (IBAction) prevAction:(id)sender {
NSLog(#"myCurrentDate===%#", myCurrentDate); // here it says
myCurrentDate = [myCurrentDate dateByAddingTimeInterval:60*60*24*-1];
[self formatDateAndPostOnButton];
}
When I try to print current date as below, it crash saying BAD_EXCESS
NSLog(#"myCurrentDate===%#", myCurrentDate);
Below is the screenshot for the same.
I'm not using ARC in my project.
Any idea what is going wrong?
Since you are not using ARC, easiest way to retain objects is to use generated setters/getters.
Instead of:
#interface MyViewController () {
NSDate *myCurrentDate;
}
make
#interface MyViewController ()
#property(nonatomic, retain) NSDate* myCurrentDate;
#end
So it will keep NSDate retained. Right now your NSDate gets deallocated when the auto-release pool is drained.
You will need to use the getters/setters provided, however:
self.myCurrentDate = [self.myCurrentDate dateByAddingTimeInterval:60*60*24*-1];
Anyways I would recommend start using ARC to make your life simpler and avoid strange memory crashes.
I'm trying to pass an NSString from ViewController to secondViewController, but the value returns null. I'm using Storyboards and a present modally segue that fires when the selectDateButton is pressed.
The user selects a date using a UIDatePicker, the value is then formatted to MM/dd and put into an NSString ivar. I'm taking that ivar and setting it to a property in my secondViewController. I'm pretty sure my mistake's probably in the way I've written it in the prepareForSegue section.
I've also tried writing it as svc.theDate = [NSString stringWithFormat:#"%#", selectedDateString]; to no avail.
I have no idea why this is happening and would appreciate some help.
ViewController.h
#import "secondViewController.h"
#interface ViewController : UIViewController {
UIDatePicker *datePicker;
NSDateFormatter *dateFormatter;
NSDate *selectedDate;
NSString *selectedDateString;
}
ViewController.m
- (IBAction)selectDateButton:(id)sender {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd"];
selectedDate = [datePicker date];
selectedDateString = [dateFormatter stringFromDate:selectedDate];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"toSecondViewController"]) {
secondViewController *svc = [segue destinationViewController];
svc.theDate = selectedDateString;
}
}
secondViewController.h
#import "ViewController.h"
#interface secondViewController : UIViewController {}
#property (nonatomic, strong)NSString *theDate;
- (IBAction)button:(id)sender;
#end
secondViewController.m
- (IBAction)button:(id)sender {
NSLog(#"Selected Date: %#", _theDate);
}
The problem is that instance variables in Objective-C by default have the
#protected
storage qualifier. That means only the class itself and its subclasses can access the instance variable.
However, what you are actually doing in prepareforSegue is this:
svc.theDate = selectedDateString;
i.e. you are passing a reference to the selectedDateString instance variable via a setter method to the BirthDayScreen view controller instance, which does not have access to selectedDateString instance variable. Why? Because it is not related in any way to the ViewController class and the variable itself is not public.
Later when you try to possibly show the string in UI, you actually have a reference to the ivar in the ViewController instance but it is not accessible so you get null.
You could explicitly make it public in ViewController
#interface ViewController : UIViewController {
UIDatePicker *datePicker;
NSDateFormatter *dateFormatter;
NSDate *selectedDate;
#public NSString *selectedDateString;
}
But actually this is not the right approach.
Instead, try changing the storage qualifier for the theDate property to
#property (nonatomic, copy)NSString *theDate;
This will create a new instance of NSString that will be a copy of the NSString represented by the selectedDateString variable, but this new copied instance will belong to the SecondViewController.
If it is not enough and still not work (it should) , instead of having instance variables in the ViewController header, create a proper public property called selectedDateString.
#interface ViewController : UIViewController {
UIDatePicker *datePicker;
NSDateFormatter *dateFormatter;
NSDate *selectedDate;
}
#property (nonatomic, strong) NSString *selectedDateString;
This way you will expose the value of the backing _selectedDateString to the outside world.
#import "AppDelegate.h"
#property (nonatomic,strong) NSString *theDate;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *str = [NSString StringWithFormate:#"%#",appDelegate.imageStringName];
NSLog(#"str : %#",str);
}
Okay, I am building an app for iOS and I am having some trouble with getting the current time to display properly within a UILabel.
Here is the code in the ViewController.h file:
#interface ViewController : UIViewController <UIApplicationDelegate>
#property (strong, nonatomic) IBOutlet UILabel *timeLabelStandbyScreen;
-(void)updateLabel;
-(void)updateTime;
#end
#interface updateTime : NSDate
#end
I'm new to Obj-C and I was playing around with it to see if I could fix the problem. Everything is fine in this file it's the .m that has the problems.
ViewController.m:
#interface ViewController ()
#end
#implementation ViewController
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateLabel];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)updateTime
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm"];
_timeLabelStandbyScreen.text = [dateFormat stringFromDate: [NSDate date]];
[self preformSelector:#selector(updateTime) withObject:self afterDelay:1.0];
}
#end
I'm sure the problem is really simple and I appreciate your help. Thanks!
By commenting out the preformSelector method and adding a NSTimer (like #rocir suggested) it correctly displayed the time.
I have looked through the other topics on this matter and I have not been able to determine my error.
I am very new to IOS programming. I am trying to create a program that looks at the selected state of 2 buttons and determines whether the buttons selected state are the same.
I am currently trying to use a model to determine the buttons selected state and then pass the state to a label. I have an error which says:
No visible #interface for 'MatchTest' declares the selector 'doesItMatch'
I'd appreciate any help that may be offered.
Thanks!
this is the MatchTest.h file
// MatchTest.h
#import <Foundation/Foundation.h>
#interface MatchTest : NSObject
#end
this is the MatchTest.m file
// MatchTest.m
#import "MatchTest.h"
#implementation MatchTest
-(NSString*)doesItMatch:(UIButton *)sender
{
NSString* tempString;
if(sender.isSelected)
{
tempString = #"selected";
}
else
{
tempString = #"not selected";
}
return tempString;
}
#end
this is the MatchViewController.h file
// MatchViewController.h
#import <UIKit/UIKit.h>
#import "MatchTest.h"
#interface MatchViewController : UIViewController
#end
this is the MatchViewController.m file
// MatchViewController.m
#import "MatchViewController.h"
#interface MatchViewController ()
#property (weak, nonatomic) IBOutlet UILabel *matchLabel;
#property (strong, nonatomic) MatchTest *match;
#end
#implementation MatchViewController
-(MatchTest *)match
{
if(!_match) _match = [[MatchTest alloc] init];
return _match;
}
- (IBAction)button:(UIButton *)sender
{
sender.selected = !sender.isSelected;
self.matchLabel.text = [self.match doesItMatch:sender];
}
#end
declare doesItMatch method in MatchTest.h file
like
in MatchTest.h
-(NSString*)doesItMatch:(UIButton *)sender;
compiler is not able to file doesItMatch method's declaration in .h file that's why that error is there.
You have not defined your method -(NSString*)doesItMatch:(UIButton *)sender in MatchTest.h file.
You are importing MatchTest.h file in MatchViewController.h file so you need to define your methods or variables or property to make available this method.
Therefore according to your error log viewController wasn't able to find the interface where this method is declared.
I have faced a problem I can not see the problem to. I am trying to pass a simple NSString to a child variable but it continues to return as null even when I use NSLog to show there is a string in the variable.
The variable finalDate will not pass to the child view.
Parent View
ChangeTimeViewController *ChangeTimeView = [[ChangeTimeViewController alloc] init];
NSLog(#"%#", date);
ChangeTimeView.finalDate = date;
[ChangeTimeView setDelegate:self];
[self.navigationController pushViewController:ChangeTimeView animated:YES];
Child View .H
#import <UIKit/UIKit.h>
#protocol ChangeTimeViewControllerDelegate;
#interface ChangeTimeViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, weak) id <ChangeTimeViewControllerDelegate> delegate;
#property (nonatomic) NSString *enteredTime;
#property (nonatomic, strong) UIPickerView *UIPicker;
#property (nonatomic, strong) NSString *finalDate;
#end
#protocol ChangeTimeViewControllerDelegate <NSObject>
- (void)childTimeViewController:(ChangeTimeViewController *)viewController didChooseValue:(NSString *)string;
#end
Child View .M
NSLog(#"%#", self->finalDate);
What you are doing is perfectly fine. You should insert the NSLog in the view(Did/Will)Appear or some similar method and you may use the self.finalDate notation to make sure you don't try to read some uninitialized ivar.
Note: properties synthesize ivars with _ as prefix (_finalDate is the correct storage unless you synthesized it it with some other name)
If you want to make sure that all input parameters are passed to the view controller, then create an init method for it. Similar to this:
- (id)initWithDate:(NSDate*)date delegate:(id)delegate
Pass NSString As ChangeTimeView.finalDate = #"This Is my Simple String"; and use/put NSLog in viewDidLoad method for show is it rich at nextViewController or not ?? Otherwise if your date (NSString) is proper then Your code is correct.
Check what is happening if you set like,
ChangeTimeView.finalDate = #"MyString";
and in view.m log NSLog(#"%#", self.finalDate);