when we touch the tabbaritem of the tabbarcontroller the delegate methods are called:
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
but when try to do the same thing programmatically, i.e.
[self.tabbarController setSelectedIndex:selectedIndexNo];
or
[self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers objectAtIndex:0]];
the delegate methods are not called. What is the reason for that?
override UITabBarController setSelectedIndex:
-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
//must call super function.
[super setSelectedIndex:selectedIndex];
[self myMethod];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self myMethod];
}
When you are setting them yourself via code, than you are aware that this is the time when the delegate method will be called. so whatever you wish to do you can do it at the time of setting the index programmatically. Say you want to call a method aMethod on tabbardelegate being called. you can call the method as soon as you set the index.
[self.tabbarController setSelectedIndex:selectedIndexNo];
[self aMethod];
Related
I have a class which was the delegate for NSTextViews, for which I was interested in the textDidEndEditing: method. I now want it to also be the delegate for NSTextFields. Problem is, they both use the same method for signaling end of text editing.
I tried to "fork" my textDidEndEditing: method to deal with both NSNotifications, but it seems like the latest (NSTextFields) don't trigger any message.
Should I be looking for an inside bug, or it is a known limitation ?
- (void)textDidEndEditing:(NSNotification *)aNotification
{ if ([[aNotification object] isKindOfClass:[NSTextView class]])
{
}
else if ([[aNotification object] isKindOfClass:[NSTextField class]])
{
}
}
Change your method and try below:-
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
Due to the neeeds of the aplication I need to call a method from a cell class of a collection view. The method is in a detail view controller which implementes the collection view .
If I call the method completely from viewDidLoad of the detail view controller at aplication start everything goes well and the button is added in the navigation bar as intended, but when I call the same method from the cell class nothing happens. Do I forget something?
Here is the code
collectionViewCell.m
- (void) addButtonToNavigationBar {
DetailViewController *dvc = [[DetailViewController alloc]init]
[dvc implementButton];
}
DetailViewController.h
- (void) implementButton;
DetailViewController.m
- (void) implementButton {
addButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(editTable)];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];
[self.navigationItem setRightBarButtonItem:addButton animated:YES];
Thank you for any hint.
When you write DetailViewController *dvc = [[DetailViewController alloc]init]; you are creating a new controller. It's not the one that already exists.
For your situation, you might consider having the controller listen for a NSNotification from the cell and call implementButton when it receives it.
my NSOutlineView outlineViewSelectionDidChange method will not be called.
I set set the NSOutlineViews delegate to the class where the other methods such as
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
exist. But outlineViewSelectionDidChange will not be called on selecting an item.
Does anybody has an idea?
This notification is a bit odd, in that it is not automatically forwarded to delegates. Try adding an explicit registration to your initialization code, like this example:
- (void)windowControllerDidLoadNib:(NSWindowController *)aController;
{
[super windowControllerDidLoadNib:aController];
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(outlineViewSelectionDidChange:)
name:#"NSOutlineViewSelectionDidChangeNotification"
object:outlineView];
}
Okay,
meanwhile i figured out that the "NSOutlineViewSelectionDidChangeNotification" will be thrown only within the notification object. So i had to subclass my NSOutlineView to catch the notification and pass it to the object where i need it.
Your own view needs to conform to the NSOutlineViewDelegate protocol like so..
#interface MyOutlineViewController : NSView <NSOutlineViewDataSource,NSOutlineViewDelegate> {
IBOutlet NSOutlineView *myoutlineview;
}
#end
you will have this methods in your implementation
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item;
-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item;
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item;
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;
where you setup your outlineview.
When loading this view -(void)viewDidLoad gets called and your predefined nib/xib file or your manual call will set your datasource to fill it depending on your logic.
Now in your -(void)viewDidLoad your myoutlineview needs to set its own delegate with
[myoutlineview setDelegate:self];
so your own View may know where to call its notification methods triggerd from selections and so on. So you can place your notification logic inside the same View class conforming to this protocol.
-(void)outlineViewSelectionDidChange:(NSNotification *)notification {
NSLog(#"selection did change");
}
I am brand spanking new to Cocoa programming, and am still kind of confused about how things wire together.
I need a pretty simple application that will fire off a single command (let's call it DoStuff) whenever any point on the window is clicked. After a bit of research it looks like subclassing NSView is the right way to go. My ClickerView.m file has this:
- (void)mouseDown:(NSEvent *)theEvent {
NSLog(#"mouse down");
}
And I have added the View to the Window and have it stretching across the whole thing, and is properly writing to the log every time the window is clicked.
I also have my doStuff method on my controller (this could be refactored to its own class I suppose, but for now it works):
- (IBAction)doStuff:(id)sender {
// do stuff here
}
So, how do I get mouseDown in ClickerView to be able to call DoStuff in the controller? I have a strong .NET background and with that, I'd just have a custom event in the ClickerView that the Controller would consume; I just don't know how to do that in Cocoa.
edit based on Joshua Nozzi's advice
I added an IBOutlet to my View (and changed it to subclass NSControl):
#interface ClickerView : NSControl {
IBOutlet BoothController *controller;
}
#end
I wired my controller to it by clicking and dragging from the controller item in the Outlets panel on the View to the controller. My mouseDown method now looks like:
- (void)mouseDown:(NSEvent *)theEvent {
NSLog(#"mouse down");
[controller start:self];
}
But the controller isn't instantiated, the debugger lists it as 0x0, and the message isn't sent.
You could either add it as an IBOutlet like Joshua said, or you could use the delegate pattern.
You would create a Protocol that describes your delegate's methods like
#protocol MyViewDelegate
- (void)doStuff:(NSEvent *)event;
#end
then you'd make your view controller conform to the MyViewDelegate protocol
#interface MyViewController: NSViewController <MyViewDelegate> {
// your other ivars etc would go here
}
#end
Then you need to provide the implementation of the doStuff: in the implementation of MyViewController:
- (void)doStuff:(NSEvent *)event
{
NSLog(#"Do stuff delegate was called");
}
then in your view you'd add a weak property for the delegate. The delegate should be weak, so that a retain loop doesn't form.
#interface MyView: NSView
#property (readwrite, weak) id<MyViewDelegate> delegate;
#end
and then in your view you'd have something like this
- (void)mouseDown:(NSEvent *)event
{
// Do whatever you need to do
// Check that the delegate has been set, and this it implements the doStuff: message
if (delegate && [delegate respondsToSelector:#selector(doStuff:)]) {
[delegate doStuff:event];
}
}
and finally :) whenever your view controller creates the view, you need to set the delegate
...
MyView *view = [viewController view];
[view setDelegate:viewController];
...
Now whenever your view is clicked, the delegate in your view controller should be called.
First, your view needs a reference to the controller. This can be a simple iVar set at runtime or an outlet (designated by IBOutlet) connected at design time.
Second, NSControl is a subclass of NSView, which provides the target/action mechanism machinery for free. Use that for target/action style controls. This provides a simple way of setting the reference to your controller (the target) and the method to call when fired (the action). Even if you don't use a cell, you can still use target/action easily (NSControl usually just forwards this stuff along to its instance of an NSCell subclass but doesn't have to).
you can also use a selector calling method,
define two properties in custom class:
#property id parent;
#property SEL selector;
set them in view controller:
graph.selector=#selector(onCalcRate:);
graph.parent=self;
and call as:
-(void)mouseDown:(NSEvent *)theEvent {
[super mouseDown:theEvent];
[_parent performSelector:_selector withObject:self];
}
I tried to call [self setNeedsDisplay:YES] in a NSTimer selector in order to trigger the drawRect method.
At first, I put the NSTimer init code in a button func:
-(IBAction)buttonPush:(id)sender
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(myTimerAction:)
userInfo:nil
repeats:YES];
}
-(void)myTimerAction:(NSTimer *) timer
{
[self setNeedsDisplay:YES];
}
The "setNeedsDisplay" is called normally but the code inside the drawRect is never called:
- (void)drawRect:(NSRect)dirtyRect
{
NSLog(#"drawRect");
}
Then I tried to move the NSTimer init code to "- (id)initWithFrame:(NSRect)frame", then everything works just fine.
(the drawRect is called correctly every 1 sec).
What's the difference between the two methods above?
What should I do if I want to trigger the Timer in a button?
Just wondering, in what class does that code reside? I would assume the buttonPush: action is inside a controller, correct?
If so, then you should have:
-(void)myTimerAction:(NSTimer *) timer
{
[[self view] setNeedsDisplay:YES];
}
because setNeedsDisplay: is a method of NSView, not NSViewController.
(BTW probably the reason why it works if you put it inside initWithFrame: is because that one is a NSView initializer: I'm guessing that when you move the code there you are also moving the myTimerAction: method, which then has "self" referring correctly to the view.)
Since it works when you use initWithFrame:, the problem is probably that buttonPush: isn't hooked up correctly. Try setting a breakpoint in buttonPush: an see if it is actually called when you click the button.