Xcode - how to use a delegate method to hide-unhide buttons - delegates

I would Like to know how to use a delegate method to change a button from hidden to unhidden. I haven't used app delegates before and they look really confusing.

Ok you have VC1 -> VC2
you have to make a button buttonTohide in VC1 hide from an action(someAction) in VC2
1.Add the delegate protocol definition in the header
#protocol CustomDelegate <NSObject>
-(void)hideUnhidebutton:(BOOL)value;
#end
2.in VC1.h make VC1 as a delegate reciever
#interface VC1<CustomDelegate>
3.in VC1.m
implement the method to execute buttonhide
-(void)hideUnhidebutton:(BOOL)value
{
[self.buttonTohide setHidden:value];
}
4.in VC2
add delegate variable as property
#property (nonatomic, strong) id<CustomDelegate> delegatePpty;
in VC2.m at someaction you have to hide button in VC1 so call the delegate method
-(void)someAction
{
[self.delegatePpty hideUnhidebutton:YES];//Call the delegate method to execute
}
and it will hide the button for you.Happy coding :)

Related

Delegate not working, when using a modal presented NavigationController ( iOS 5 - Storyboard )

I have a problem with a protocol.
My "initial View Controller" is a Navigation Controller. On the root page i show an other Navigation Controller in which is View Controller embedded. onclick a segue should be fired...this works perfectly but the delegate method from the "ViewController" is never called.
The image I added is a example how I build the connection between the 2 NavigationControllers with the InterfaceBuilder in iOS 5.
MyViewController.h
#protocol MyProtocol <NSObject>
#required
- (void) myFunction:(NSString *)string;
#end
#interface MyViewController : UIViewController <MyProtocol>
#property (nonatomic, assign) id<MyProtocol> delegate;
#end
MyViewController.m
#import "MyViewController.h"
#implementation PropertyController
#synthesize delegate = _delegate;
- (void) myFunction:(NSString *)string {
[_delegate myFunction:string];
}
- (IBAction) callDelegate:(id)sender {
![enter image description here][1][self myFunction:#"test"];
}
And this is the code for the ViewController which is showing the NavigationController from above
ViewController.h
#import "MyViewController.h"
#interface ViewController : UIViewController <MyProtocol>
ViewController.m
#import "ViewController.h"
#implementation ViewController
- (void) myFunction:(NSString *)string {
NSLog(#"myFunction was called");
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[((MyViewController *) segue.destinationViewController) setDelegate:self];
}
- (IBAction) showModalNavigationController {
[self performSegueWithIdentifier:#"NameFromSegueInInterfaceBuilder" sender:self];
}
I cant find a solution for my problem.
I hope somebody can help me
thank you :)
I see a couple of problems here:
In your storyboard screenshot, you have a second navigation controller. You should not not need to embed your PropertyController in a navigation controller. Instead, have the root view controller segue directly to the PropertyController. If for some reason you do need that navigation controller, then you would need to change your prepareForSegue implementation above, because segue.destinationViewController in this case points to the UINavigationController. So you would need to get that nav controller object, and then send setDelegate to the rootViewController of that nav controller object. But again, only if you decide to keep that navigation controller.
How does MyViewController relate to your ViewController and PropertyController classes? The PropertyController class (or a superclass) needs to have the #property and synthesize statements for the delegate property.
I've been having the same issue, to make it work, I found a work around which xcode does not really like, but it still is working though --> setting your delegate as you do it, makes your navigation controller have your view as delegate, if you NSLog self.delegate in your destination controller it will be null. To prevent this do -->
self.delegate = self.navigationController.delegate;
in your destination controller (viewdidload), it will get the delegation you created on the navigation controller, and allow you yo use it in your destination controller
It is dirty but it's the only way I found. Hope it helps.

How does an NSView subclass communicate with the controller?

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];
}

How to clear a text field in Cocoa

I'm trying to make a simple Cocoa application using the newest version of XCode. In interface builder I added NSTextField and NSButton. When I press the button, I want it to clear whatever is in the text field.
I made a new class called AppController.h. This is the contents:
#import <Foundation/Foundation.h>
#interface AppController : NSObject {
IBOutlet id textView;
}
- (IBAction) clearText: sender;
#end
AppController.m looks like this:
#import "AppController.h"
#implementation AppController
- (IBAction) clearText: sender
{
[textView setString: #" "];
}
#end
I connected the button to clearText and the textbox to textView.
The program compiles and runs. But when I press the button, nothing happens. Why is that?
here's the run down.
1.Create an IBAction in your appController class header.
- (IBAction)someMethod:(id)sender;
2.Then create an IBOutlet for you text field
IBOutlet NSTextField *textFieldname;
You then connect the IBAction to the button in interface builder, and your IBOutlet to your textfield.
Inside the implementation file (.m) IBAction method do
- (IBAction)someMethod:(id)sender{
textFieldname.stringValue = #"";
}
This is very basic. I suggest you google for some tutorials. There's plenty out there. Here's something that may help. Chapter 8 describes how to do exactly what you're asking.
link text

What's wrong with this Cocoa code?

I'm trying to make a simple Cocoa application using XCode 3.2.3. In interface builder I added NSTextField and NSButton. When I press the button, I want it to clear whatever is in the text field.
I made a new class called AppController.h. This is the contents:
#import <Foundation/Foundation.h>
#interface AppController : NSObject {
IBOutlet id textView;
}
- (IBAction) clearText: sender;
#end
AppController.m looks like this:
#import "AppController.h"
#implementation AppController
- (IBAction) clearText: sender
{
[textView setString: #" "];
}
#end
I connected the button to clearText and the textbox to textView.
The program compiles without error and runs. But when I press the button, nothing happens. Why is that?
Using id for an IBOutlet is a bad practice. Use
IBOutlet NSTextView* textView;
instead.
Please check using the debugger, or putting NSLog(#"foo!"); before [textView setString:#""] to see if the action method is really called.
Another pitfall is that there are NSTextView and NSTextField. These two are different!
The former supports both setString: and setStringValue:, while the latter only supports setStringValue:.
Which object did you use in the interface builder?

Which delegate method should I use to respond to clicks on an NSTextField?

I am trying to respond to a click within a textfield. When the click occurs, I am going to open a panel. My initial thought was to use a delegate method to respond to the click event - but I found that:
This method doesn't work:
(void)textDidBeginEditing:(NSNotification *)aNotification
This method does work, but only when I actually edit the text within the text field, not when I first click it. And - if I edit the text a second time, this method stops working:
(void)controlTextDidBeginEditing:(NSNotification *)aNotification
I could use as much detail as possible - or a code example, ideally. I know that an nstextfield inherits from NSControl, which has a mouseDown event. Is there a similar way to respond to the event with a textfield, also?
Since NSTextField inherits from the NSControl class, it also inherits the -(void)mouseDown:(NSEvent*) theEvent method.
I needed to have an NSTextField call a delegate function upon clicking it today, and thought this basic code might be useful. Note that NSTextField already has a delegate and that in SDK v10.6, the delegate already has a protocol associated with it. Note that if you don't care about protocols, compiler warnings, etc., you don't need the protocol and property declarations or the getter and setter.
MouseDownTextField.h:
#import <Appkit/Appkit.h>
#class MouseDownTextField;
#protocol MouseDownTextFieldDelegate <NSTextFieldDelegate>
-(void) mouseDownTextFieldClicked:(MouseDownTextField *)textField;
#end
#interface MouseDownTextField: NSTextField {
}
#property(assign) id<MouseDownTextFieldDelegate> delegate;
#end
MouseDownTextField.m:
#import "MouseDownTextField.h"
#implementation MouseDownTextField
-(void)mouseDown:(NSEvent *)event {
[self.delegate mouseDownTextFieldClicked:self];
}
-(void)setDelegate:(id<MouseDownTextFieldDelegate>)delegate {
[super setDelegate:delegate];
}
-(id)delegate {
return [super delegate];
}
AppDelegate.h:
#interface AppDelegate <MouseDownTextFieldDelegate>
...
#property IBOutlet MouseDownTextField *textField;
...
AppDelegate.m:
...
self.textField.delegate = self;
...
-(void)mouseDownTextFieldClicked:(MouseDownTextField *)textField {
NSLog(#"Clicked");
...
}
...
If you're building with 10.5 SDK, don't have the protocol inherit from NSTextFieldDelegate.

Resources