Subclassing UILocalNotification for property addition - properties

I tried to cancel localNotifications with specific tag number. It's my favorite manner to use tag number for compare logic.
So, I subclassed UILocalNotification and add property NSInteger tag. Then use the subclass instance for scheduling localNotification.
CustomNotification *customNoti = [[CustomNotification alloc] init];
customNoti.fireDate = myDate;
customNoti.timeZone = [NSTimeZone defaultTimeZone];
customNoti.tag = self.tag; // crash
// error : -[UIConcreteLocalNotification setTag:]: unrecognized selector sent to instance 0x75a3630
Here is CustomNotification.h file
#import <UIKit/UIKit.h>
#interface CustomNotification : UILocalNotification
#property (nonatomic, assign) NSInteger tag;
#end
Why is it crash and 'unrecognized selector' message shown? Is UILocalNotification class special? I can't find any missed code.

Related

Error when adding object to Context in seperate Controller

I have a simple app with AppDelegate and MainController - I have passed the managedObjectContext to the MainController (I think successfully) but I receive an error when added an object to the context.
Code:
#implementation AppDelegate
-(void)applicationDidFinishLaunching:(NSNotification *) aNotification
{
// this line is wrong: MainController *controller = [[MainController alloc] init];
controller.managedObjectContext = self.managedObjectContext;
}
#interface MainController : NSObject
#property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
#implementation MainController
-(IBAction)addItem:(id)sender {
NSManagedObject *newObject = [NSEntityDescription
insertNewObjectForEntityForName:#"Person"
inManagedObjectContext: self.managedObjectContext];
//The above line gives an error
ERROR:
+entityForName: nil is not a legal NSManagedObjectContext parameter search for entity name "Person"
If I change the code and do everything in the AppDelegate, everything works without issues.
I am not sure what is going on.
[EDIT]
I needed to create an IBOutlet from the MainController object in IB to the AppDelegate - thanks Nofel.
I think it should be like this:
Person *person = [NSEntityDescription insertNewObjectForEntityForName:#"Person" inManagedObjectContext:_managedObjectContext];
[person setName:someNameTextField]; // if you want to set some properties
[_managedObjectContext save:nil]
It depends on how you create your Core-Data model and what you use in you appDelegate.
Best way is to watch some tutorial or Developer Library first.

Label update is null

I'm having an issue with a label not updating correctly in my UI. I have an Mac OS X app which uses a outline view to switch views. I want to simply have the date displayed to the user in a label on the view which is switched to (FirstViewController). When implemented alone in a new project I have no issue. But when implemented where the view changes, the value of the label does not update, in fact the console output indicates that _dateLabel is (null) even after being set prior. Any suggestions? I must be missing something quite fundamental!
Console output:
2014-08-30 19:54:22.719 OutlineView[10420:1613022] StringedText is 30 August 2014
2014-08-30 19:54:22.720 OutlineView[10420:1613022] label value is (null)
I include the following code:
//
// FirstViewContorller.h
// OutlineView
#import <Cocoa/Cocoa.h>
#interface FirstViewContorller : NSViewController
#property (weak) IBOutlet NSTextField *dateLabel;
-(void)updateDateScreen;
#end
//
// FirstViewContorller.m
// OutlineView
#import "FirstViewContorller.h"
#implementation FirstViewContorller
#synthesize dateLabel = _dateLabel;
-(void)updateDateScreen{
//date calculation for main window
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString *stringedText = [formatter stringFromDate:now];
_dateLabel.stringValue = stringedText;
NSLog(#"StringedText is %#", stringedText);
NSLog(#"label value is %#", _dateLabel.value);
}
#end
//
// AppDelegate.m
// OutlineView
#import "AppDelegate.h"
#import "Book.h"
#import "FirstViewContorller.h"
#interface AppDelegate()
#property (weak) IBOutlet NSOutlineView *outlineView;
#property (weak) IBOutlet NSTreeController *booksController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//other code here
//Call the update method in FirstViewController to load date label
FirstViewContorller *instance = [[FirstViewContorller alloc]init];
[instance updateDateScreen];
}
//further unrelated code
#end
You have:
NSLog(#"label value is %#", _dateLabel.value);
And because it is outputting "null" you think that your value is "null" when in all likelihood it is _dateLabel that is null.
You are creating the instance object, but then calling a method that updates a UI object, which may not have been unarchived from the xib file by the time you are calling it. So although your date formatter is correctly creating a string, it is trying to set it on a nil object.
You can see this for yourself by examining the output of of:
NSLog(#"label is %#", _dateLabel);
which will probably return a "null" as well.

Extending UILocalNotification [duplicate]

I beginner in iOS. I try extend UILocalNotification. My class below.
#interface FSCustomNatification : UILocalNotification
typedef enum {
FSCustomNatificationPay,
FSCustomNatificationWrite,
FSCustomNatificationSend
} NotificationTypeT;
#property (nonatomic, assign) NotificationTypeT typeNotificationT;
#end
#import "FSCustomNatification.h"
#implementation FSCustomNatification
#end
When I set typeNotificationT property I get -[UIConcreteLocalNotification setTypeNotificationT:]: unrecognized selector sent to instance 0x8144780. Why?
FSCustomNatification* localNotification = [[FSCustomNatification alloc] init];
localNotification.typeNotificationT = FSCustomNatificationWrite;
By the looks of your error message UILocalNotification is part of a class cluster. The docs don't say (that I can see quickly) but it seems unlikely that you should subclass UILocalNotification.
Instead, you should make use of the userInfo provided by UILocalNotification to add any extra information you want to be available when the notification fires.

Crash on NSMutableDictionary's setObject function

Here is my code:
NSLog(#"dictionaryPlayers=%#,%d",[dictionaryPlayers description],dictionaryPlayers.count);
[dictionaryPlayers setObject:#"test" forKey:#"test2"];
dictionaryPlayers is inited in this class's init function:
-(id)init{
...
dictionaryPlayers = [[NSMutableDictionary dictionaryWithCapacity:10]retain];
...
}
The program crashed:
Thread 1:Program received signal: "SIGABRT".
And in console:
2011-12-27 17:01:21.744 [25454:207] dictionaryPlayers={
},0
2011-12-27 17:01:21.745 [25454:207] -[__NSCFConstantString tick]: unrecognized selector sent to instance 0x199bcc
With the NSLog outputs, i think dictionaryPlayers is well inited. So I don't know why crashed...
The object on which you call tick: is not longer in memory and causes this crash. Try to see why this object is released.
It seems that this is not a local case, so did you make sure to synthesize it at the top? And declared it correctly in the header? Examples:
In header:
#property (nonatomic, strong) NSMutableDictionary *dictionaryPlayers;
in class:
#synthesize dictionaryPlayers = _dictionaryPlayers;
I removed
dictionaryPlayers = [NSMutableDictionary dictionaryWithCapacity:10];
from init(), and added
dictionaryPlayers = [[NSMutableDictionary alloc] init];
above my log statement. Still crash....
Then I removed
[dicTest setValue:#"Test" forKey:#"testKey"];
So there's only 2 lines left:
dictionaryPlayers = [[NSMutableDictionary alloc] init];
NSLog(#"dictionaryPlayers=%#,%d",[dictionaryPlayers description],dictionaryPlayers.count);
It didn't crash. So, it seems the setValue line really is the problem.
You don't need to call retain on the object in your init statement. Also just for giggles try:
dictionaryPlayers = [[NSMutableDictionary alloc] init];
instead of
dictionaryPlayers = [NSMutableDictionary dictionaryWithCapacity:10];
And do this right above your log statement (take it out of the init).
If this works, put a log in your init method and make sure that is being called before your method that adds KV's to the dictionary
I cannot reproduce this behavior. Here is my code:
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property(nonatomic, retain) NSMutableDictionary *dictionaryPlayers;
#property (retain, nonatomic) IBOutlet UITextView *logTextView;
- (IBAction)logButtonPressed:(id)sender;
#end
ViewController.m:
#import "ViewController.h"
#implementation ViewController
#synthesize dictionaryPlayers;
#synthesize logTextView;
#pragma mark - My Methods
- (IBAction)logButtonPressed:(id)sender {
logTextView.text = [NSString stringWithFormat:#"%#,%d",[dictionaryPlayers description],[dictionaryPlayers count]];
NSLog(#"dictionaryPlayers=%#,%d",[dictionaryPlayers description],[dictionaryPlayers count]);
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dictionaryPlayers = [[NSMutableDictionary alloc] init];
[dictionaryPlayers setValue:#"Test" forKey:#"testKey"];
NSLog(#"dictionaryPlayers=%#,%d",[dictionaryPlayers description],[dictionaryPlayers count]);
}
No issues. If you are doing things this way, you cannot have problems. Here are the steps I would take to troubleshoot further:
Do a project search for playersDictionary and make sure there isn't something else touching that object.
Try cleaning the project
Create a new project with this structure and see what happens

EXC_BAD_ACCESS iPhone Dev

I am very new to development in Objective C but have a lot of experience in object orientated development in c# ASP. I've been trying to make a simple iPhone app where a PickerView control is accessing a data source.
In my header file (InstaTwitViewController.h) I have declared the following:
#interface InstaTwitViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* activities;
NSArray* feelings;
}
I now try to inititialise my arrays in the viewDidLoad functions in my InstaTwitViewController.m file:
activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"thinking", nil];
feelings = [[NSArray alloc] initWithObjects:#"psyched", #"sad", #"happy", nil];
but when I run the code on the simulator I get a Thread 1: Program recieved signal "ESC_BAD_ACCESS" error on the first line of code (where I allocate data to the activities array).
I put a break point on the line and it recognises it as an NSArray called activities but says "Invalid Summary" at the end.
Does anyone know why I am getting this error? I have looked at many threads about the ESC_BAD_ACCESS error but none have seemed to have helped me.
.h
#interface InstaTwitViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* activities;
NSArray* feelings;
}
#property (nonatomic, retain) NSArray* activities;
#property (nonatomic, retain) NSArray* feelings;
#end
.m
#import "your.h"
#implementation InstaTwitViewController
#synthesize activities, feelings;
- (void)viewDidLoad {
[super viewDidLoad];
[self.activities addObject ~ /* Do your code here */];
}
#end
Becareful you should stay on self.~ if you don't want to lose your value.
By the way, I am Korean~ ^^;