How to access dataSource outlet class from UITableView? - objective-c

i have one MYViewController which contains MYTableView, with settings:
1) dataSource outlet is delegated to MYDataSource
2) delegate of MYTableView is set to MYTableView
I have the code in delegate class
//MYTableView (delegate is set in storyboard)
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
//code here
[(MYDataSource *) self.dataSource loadMoreWithSuccess:^(){
[self reloadData];
}];
}
but, the dataSource loadMoreWithSuccess cant be accessed, how to call it?
UPD
So, after some debugging, i've ended up with following:
//MYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initTable];
}
-(void) initTable{
NSLog(#"dataSource: %s", class_getName([_table.dataSource class]));
// dataSource: MYDataSource
NSLog(#"delegate: %s", class_getName([_table.delegate class]));
// delegate: MYTableView
}
but in MYTableView when i do the following, i get
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
NSLog(#"dataSource: %s", class_getName([self.dataSource class]));
// dataSource: nil
NSLog(#"delegate: %s", class_getName([self.delegate class]));
// delegate: nil
}
Maybe i didn't notice something important in the Apple Developer Guide?

Related

Why is my storyboard not loaded?

I am trying to open a separate storyboard using code for a specific login task.
I created the storyboard and the instance class as follows
+ (instancetype)newStoryboardInstance {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([self class]) bundle:[NSBundle bundleForClass:[self class]]];
AdyenLoginViewController *viewController = [storyboard instantiateInitialViewController];
return viewController;
}
I am then calling this from another class like this
- (void) viewDidAppear:(BOOL)animated {
if([NWTillHelper isDebug] == 1) {
NSLog(#"%s entered", __PRETTY_FUNCTION__);
}
[AdyenLoginViewController newStoryboardInstance];
}
But the loginController is never shown and no error messages are shown, it just finishes loading my normal storyboard and then stops.
The storyboard name etc is correct as per the class name and no compiler errors or warnings are shown
How can I make it load my specific storyboard?
If you are using navigation controller then viewDidAppear will look like this :-
- (void) viewDidAppear:(BOOL)animated {
if([NWTillHelper isDebug] == 1) {
NSLog(#"%s entered", __PRETTY_FUNCTION__);
}
if([AdyenLoginViewController newStoryboardInstance]) {
[self.navigationContoller pushViewController:[AdyenLoginViewController newStoryboardInstance] animated:true];
}
}
Or you can present the viewController like this :-
- (void) viewDidAppear:(BOOL)animated {
if([NWTillHelper isDebug] == 1) {
NSLog(#"%s entered", __PRETTY_FUNCTION__);
}
if([AdyenLoginViewController newStoryboardInstance]) {
[self presentViewController:[AdyenLoginViewController newStoryboardInstance] animated:true completion:^{
}];
}
}

implementing Singleton for xib, with initWithCoder and delegate

so my problem is this:
I'm trying to have a singleton for a custom view xib. When I fire a notification it's not getting caught by the observer, because apparently my combobox has a different 'self' than '_sharedInstance'.
here is the code - initWithCoder:
-(id)initWithCoder:(NSCoder *)coder{
if (_sharedInstance){
self = _sharedInstance;
return self;
}else{
_sharedInstance = [super initWithCoder:coder];
return _sharedInstance;
}
}
awakeFromNib: (because in initWithCoder: _sharedInstance.cmbBox is nil)
-(void)awakeFromNib{
_sharedInstance.cmbBox.delegate = _sharedInstance;
}
The delegate that should catch the combobox selection:
// NSComboBoxDelegate:
-(void)comboBoxSelectionDidChange:(NSNotification *)notification{
if (notification.object == _sharedInstance.cmbBox){
NSLog(#"Do Something!");
[[NSNotificationCenter defaultCenter] postNotificationName:#"DoSomething" object:_sharedInstance userInfo:nil];
}
}
The problem is notification.object != _sharedInstance.cmbBox, so it never enters the if statement...
Did I do something stupid? :)

When are interface builder IBOutlet objects created

I am using the iPad master / detail project template and I'm trying to update some UILabels in the detailViewController when the app is first run.
Here is My Code :
(void)setObject:(id)newObject
{
if (_object != newObject) {
[_object release];
_object = [newObject retain];
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
[self updateDetails];
}
- (void) updateDetails
{
NSLog(#"Details = %#", self.details);
NSLog (#"detailLabel %#", self.detailLabel);
self.detailLabel.text = [self.details objectForKey:#"aKey"];
}
- (IBAction)refresh:(UIBarButtonItem *)sender {
[self updateDetails];
}
setEvent is called from the Master View Controller's viewDidLoad method as selecting it's tableview's 1st row as a default.
if (![self.tableView indexPathForSelectedRow])
{
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
[self.detailViewController setObject:[self.sortedObjects objectAtIndex:0]];
}
When I run the code the detailLabel is not changed and the NSLog says self.detailLabel is (null). If 'refresh' is called later from a button click detailLabel is not null and updates correctly. How can I do this to make sure the detailLabel has been created and is not (null)?
The IBOutlet objects will be created at the time of viewDidLoad() of that object(detail view, but not master view).
call the [self updateDetails]; in viewdidLoad() method of detail view controller to avoid this problem.

NSInvocation invocationWithMethodSignature, method signature argument cannot be nil

I followed tutorial from cocos2d official site . I try to create some items for a menu when creating them i pass a selector with one parameter. For each item i pass different selector . I think here is the problem , but i dont see realy why here is the problem. My header file looks :
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "CCTouchDispatcher.h"
// HelloWorldLayer
#interface HelloWorldLayer : CCLayer {
CCSprite *first;
CCSprite *second;
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
- (void) setUpMenus;
- (void) doSomethingOne: (CCMenuItem *) menuItem;
- (void) doSomethingTwo: (CCMenuItem *) menuItem;
- (void) doSomethingThree: (CCMenuItem *) menuItem;
#end
Implementation file :
// Import the interfaces
#import "HelloWorldLayer.h"
// HelloWorldLayer implementation
#implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
- (void) doSomethingOne: (CCMenuItem *) menuItem
{
NSLog(#"The first menu was called");
}
- (void) doSomethingTwo: (CCMenuItem *) menuItem
{
NSLog(#"The second menu was called");
}
- (void) doSomethingThree: (CCMenuItem *) menuItem
{
NSLog(#"The third menu was called");
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
first = [CCSprite spriteWithFile:#"seeker.png"];
first.position = ccp(100, 100);
[self addChild:first];
second = [CCSprite spriteWithFile:#"Icon.png"];
second.position = ccp(50, 50);
[self addChild:second];
[self schedule:#selector(nextFrame:)];
[self setUpMenus];
self.isTouchEnabled = YES;
}
return self;
}
- (void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [self convertTouchToNodeSpace: touch];
[second stopAllActions];
[second runAction: [CCMoveTo actionWithDuration:1 position:location]];
}
- (void) nextFrame:(ccTime)dt {
first.position = ccp( first.position.x + 100*dt, first.position.y );
if (first.position.x > 480+32) {
first.position = ccp( -32, first.position.y );
}
}
- (void) setUpMenus {
CCMenuItemImage *menuItem1 = [CCMenuItemImage itemFromNormalImage:#"myfirstbutton.png"
selectedImage:#"myfirstbutton_selected.png"
target:self
selector:#selector(doSomenthingOne:)];
CCMenuItemImage *menuItem2 = [CCMenuItemImage itemFromNormalImage:#"mysecondbutton.png"
selectedImage:#"mysecondbutton_selected.png"
target:self
selector:#selector(doSomenthingTwo:)];
CCMenuItemImage *menuItem3 = [CCMenuItemImage itemFromNormalImage:#"mythirdbutton.png"
selectedImage:#"mythirdbutton_selected.png"
target:self selector:#selector(doSomenthingThree:)];
CCMenu *myMenu = [CCMenu menuWithItems:menuItem1,menuItem2,menuItem3, nil];
[myMenu alignItemsVertically];
[self addChild:myMenu];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
#end
You've got the same typo in all three menu item creation calls. You're telling the menu items that the selector they should use is called doSomenthing... (note the spurious n in the middle):
CCMenuItemImage *menuItem1 = [... selector:#selector(doSomenthingOne:)];
CCMenuItemImage *menuItem2 = [... selector:#selector(doSomenthingTwo:)];
CCMenuItemImage *menuItem3 = [... selector:#selector(doSomenthingThree:)];
but the actual names of your methods are doSomethingOne:, doSomethingTwo:, and doSomethingThree:.
The exact cause of the error message is that later, when the menu item needs to perform that selector, it will ask your class to tell it the method signature for the selector you gave it. Since you gave the item an incorrect selector, your class doesn't know the signature, and it returns nil. The menu item tries to construct an NSInvocation object anyways to perform its action, which fails because the invocation can't be created with a nil signature.
Fix the typos and everything should work fine.

NSView mouseExited

I have an NSView and basically, even when my mouse doesn't leave the defined frame, just moves within it, mouseExited function is called. Is this how is it suppose to be or am I doing something wrong? There are several subviews of this NSView and it's custom and here's the code for it:
- (id)initWithDelegate:(id)del {
if (self = [super init]) {
[del retain];
delegate = del;
}
return self;
}
- (void)dealloc {
[delegate release];
[super dealloc];
}
- (void)viewDidMoveToWindow {
[self addTrackingRect:[self bounds]
owner:self
userData:nil
assumeInside:NO];
}
- (void)mouseEntered:(NSEvent *)theEvent {
[delegate mouseEntered];
}
- (void)mouseExited:(NSEvent *)theEvent {
NSLog(#"mouse exited");
[delegate mouseExited];
}
- (void)mouseDown:(NSEvent *)theEvent {
[delegate mouseDown];
}
- (NSView *)hitTest:(NSPoint)aPoint {
return self;
}
Thanks.
I figuered it out. After adding a tracking area, i was changing the frame of my view so I needed to recalculate the tracking area. Found this method that will automatically be called whenever tracking area needs to be updated:
- (void)updateTrackingAreas {
Just simply recalculate your tracking area here.