delegate does not work - iphone

I have a tableview ,and in that tableview i am setting customcell .in that customcell have a button,when button click then delegate method called and play or pause audio.
But i dont know why my method does not called.
here is my code.
AudioInfoCell.h //itas child view
#import <UIKit/UIKit.h>
#protocol PlayPauseDelegate <NSObject>
-(void)playPauseAudio;
#end
#interface AudioInfoCell : UITableViewCell
{
id<PlayPauseDelegate> delegate;
}
#property(nonatomic,strong)id delegate;
#end;
AudioInfoCell.m
#synthesize delegate;
- (IBAction)playPauseBtnTapped:(id)sender {
if([delegate respondsToSelector:#selector(playPauseAudio)])
{
//send the delegate function with the amount entered by the user
[delegate playPauseAudio];
}
}
AudioTableViewController.h //its parent view
#import "AudioInfoCell.h"
#interface AudioTableViewController : UIViewController<PlayPauseDelegate>
AudioTableViewController.m
cell.delegate = self;
-(void)playPauseAudio{
NSLog(#"button pressed");
}

#kartik , i you have not specified in which method you have specifies cell.delegate=self; .
so i assume that you have specified it in viewDidLoad,instead of that specify it in vieWillApear method than it should work.

Related

Passing value from DetailTableView to MasterTableView

In my DetailTableView users can change the label text (counter) by clicking different buttons. After clicking the back button, the MasterTableView label text should have the same value (counter). Any idea?
To get data from DetailTableView to MasterTableView, you need to create delegate method.
Write this code in MasterViewController.h file
#protocol ContentDelegate <NSObject>
- (void) showMessage;
#end
#interface MasterViewController : UITableViewController
{
}
#property(nonatomic,assign) id<ContentDelegate> delegate;
#end
Implement ShowMessage method in Detail View Controller
- (void) showMessage:(NSString*)message
{
_messageLabel.text = message;
}
Call delegate method from MasterViewController on table cell click
[_delegate showMessage:"Hello !!!"];
First Import MasterViewController in DetailViewController.h file
Then add ContentDelegate in DetailViewController.h file in given form :-
#interface DetailViewController : UITableViewController<ContentDelegate>
{
}
Also set delegate self while calling MasterViewController.
Hope this solution will help you.. Thanks.

Change UIImage view between two View Controllers

It's possible to change an image by clicking on a button inside an other View Controller?
I use this code in order to pass the data from the ViewController to the SecondViewController. I would want to add to the same button the commando to add an image to the SecondViewController... Excused my poor question...
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#interface ViewController : UIViewController <SecondViewControllerDelegate> {
IBOutlet UITextField *userNameTextField;
}
#property (nonatomic, strong) UITextField *userNameTextField;
#end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"to2"]){
SecondViewController *viewController = segue.destinationViewController;
viewController.delegate = self;
}
}
- (void)done:(NSString *)name{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Back in first ViewController, metod Done, with name=%#",name);
userNameTextField.text=name;
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#protocol SecondViewControllerDelegate <NSObject>
-(void) done:(NSString*)someText;
#end
#interface SecondViewController : UIViewController {
IBOutlet UITextField *someText;
IBOutlet UIButton *returnButton;
id delegate;
}
#property (nonatomic, assign) id <SecondViewControllerDelegate> delegate;
#property (nonatomic, strong) UITextField *someText;
- (IBAction)returnButtonPressed:(id)sender;
#end
SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize someText;
#synthesize delegate = _delegate;
- (IBAction)returnButtonPressed:(id)sender {
[self.delegate done:someText.text];
}
#end
Here is one way that should work for you:
Just set up an NSNotification in your ViewController with the image. Trigger the notification in the other viewController when the button is pressed.
in your image's view controller:
//in ViewDidLoad:
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(changeTheImage) name:#"CHANGE_THE_IMAGE" object:nil];
//then elsewhere in the class:
- (void)changeTheImage {
//change your image here
}
//and in your dealloc, make sure you add this, or it will keep "observing"
[[NSNotificationCenter defaultCenter] removeObserver:self];
Then in the button view controller:
//have this as your button action (or add the line to your button action)
- (IBAction)yourButtonWasPressed:(id)sender {
[[NSNotificationCenter defaultCenter]postNotificationName:#"CHANGE_THE_IMAGE" object:self];
What's the mean to change another UIViewController. because when you click on current UIViewController that's mean the current is on top and visible. what you called another can only be overlap inactive or it's does not exist right now.
So if you want somehow to change the UIImage from another UI, the simplest way is to hold the UIImage or image file name in a global store area such as your AppDelegate class, file. when click change the global area content. and in the ui for image show you can add some code in
- (void)viewWillAppear:(BOOL)animated {
//read and change UIImage content from global store
}
is it what you want ?

Objective-c multiple delegates in the same view - ECSlidingViewController

I started testing ECSlidingViewController and after I tried to access FirstTopViewController I have a big trouble - because in FirstToViewController I already have ZBarReaderDelegate implemented and all examples of delegate are not triggering any method from my delegate.
Basically I have this stuff:
FirstTopViewController.h
#import ...MyStuff...
#import "UnderRightViewController.h"
#interface FirstTopViewController : UIViewController <RightViewDelegate, ZBarReaderDelegate>
#property (weak, nonatomic) IBOutlet UITextView *labelTotal;
#end
FirstTopViewController.m
#import "FirstTopViewController.h"
#implementation FirstTopViewController
- (void)setTotalViewController:(UnderRightViewController*)controller didTotalChange:(NSString*)total
{
//labelTotal.text = total;
NSLog(#"I'm here!!! and received %#", total);
}
From other side I have
UnderRightViewController.h
#import <UIKit/UIKit.h>
#import "ECSlidingViewController.h"
#class UnderRightViewController;
#protocol RightViewDelegate <NSObject>
- (void)setTotalViewController:(UnderRightViewController*)controller didTotalChange:(NSString*)total;
#end
#interface UnderRightViewController : UITableViewController
#property (nonatomic, weak) id <RightViewDelegate> delegate;
#end
UnderRightViewController.m
#import "UnderRightViewController.h"
#interface UnderRightViewController ()
#end
#implementation UnderRightViewController
#synthesize delegate;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[delegate setTotalViewController:self didTotalChange:#"foo"];
}
#end
I'm trying this entire day solve this puzzle but I never get setTotalViewController fired.
Thanks in advance.
Friend you did a small mistake, when you navigate from FirstTopViewController to UnderRightViewController at that time you need to do this in FirstTopViewController.m:-
UnderRightViewController *obj = [[UnderRightViewController
alloc] initWithNibName:#"UnderRightViewController" bundle:nil];
obj.delegate = self; // u forget to assign protocol handler
[self.navigationController pushViewController:obj animated:YES];
[obj release];
You don't have any code that is setting the delegate for the UnderRightViewController. I don't know what object owns both of these controllers, but before either UnderRightViewController and FirstTopViewController are displayed it should run code something like this:
FirstTopViewController *ftvc = //... where ever you get a reference to this from
UnderRightViewController *urvc = ...;
urvc.delegate = ftvc;
In your above code you are using custom delegates and also you have used it for sending message to onecontroller class to another controller class. So below is the same sample code of custom delegates, it is working fine in similar way you have to implement and also the problem in your code is you are not setting the delegate, so please follow below how to set the same and call the method. here i have used your same method only return type i have defined as NSString in-spite of void for explaining purpose, but you can use void according to your requirement hope it will be helpful to you:-
First Controller Class AWindowController.h
#interface AWindowController : NSWindowController<sampleDelegate>
{
NSString *textA;
}
#property(readwrite,retain)NSString *textA;
-(IBAction)doSet:(id)sender;
#end
#import "AWindowController.h"
#import "BWindowController.h"
#interface AWindowController ()
#end
#implementation AWindowController
#synthesize textA;
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (NSString *)setTotalViewController:(BWindowController*)controller didTotalChange:(NSString*)total
{
NSLog(#"recieved");
return #"recieved";
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
-(NSString*)windowNibName
{
return #"AWindowController";
}
-(IBAction)doSet:(id)sender
{
[self setTextA:#"Awindow Button Pressed"];
BWindowController *b=[[BWindowController alloc]init];
b.delegate=self;
[b showWindow:self];
}
#end
Second Controller Class BWindowController.h
#import <Cocoa/Cocoa.h>
#import "sampleDelegate.h"
#class BWindowController;
#protocol sampleDelegate <NSObject>
#required
//-(NSString *)getDataValue;
- (NSString *)setTotalViewController:(BWindowController*)controller didTotalChange:(NSString*)total;
#end
#interface BWindowController : NSWindowController<sampleDelegate>
{
NSString *bTextValue;
id<sampleDelegate>delegate;
}
#property(readwrite,retain)NSString *bTextValue;
#property(readwrite,assign)id<sampleDelegate>delegate;
#end
#import "BWindowController.h"
#interface BWindowController ()
#end
#implementation BWindowController
#synthesize bTextValue,delegate;
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (NSString *)setTotalViewController:(BWindowController*)controller didTotalChange:(NSString*)total;
{
return nil;
}
- (void)windowDidLoad
{
NSString *str= [[self delegate]setTotalViewController:self didTotalChange:#"recieved"];
self.bTextValue=str;
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
-(NSString*)windowNibName
{
return #"BWindowController";
}
#end
Attached screen shot in Output:-
Below is window is the AwindowController.h class
Below in the same above window pressing the button and when Awindow button pressed data will send
and notification will be recieved in Bwindow using above define custom delegates as attached in the screen shot.

Custom Delegate Does Not Receive a Call

My custom delegate does not receive a call. Here is my setup. ViewController has a SliderView object which is a subclass of UIScrollView:
SlideView.h
#import <UIKit/UIKit.h>
#protocol SlideViewDelegate <NSObject
#required
-(void) didTapImageData:(NSMutableArray*)imageData atIndex:(int)index;
#end
#interface SliderView : UIScrollView<UIGestureRecognizerDelegate> {
__weak id<SlideViewDelegate> slideViewDelegate;
}
#property (nonatomic, weak) id<SlideViewDelegate> slideViewDelegate;
#end
SlideView.m
#import "SliderView.h"
#implementation SliderView
#synthesize slideViewDelegate;
- (void) handleTap:(UITapGestureRecognizer *)recognizer{
NSLog(#"tapped");
[[self slideViewDelegate] didTapImageData: imageData atIndex:0 ];
}
ViewController.h
#interface ViewController : UIViewController <
UIScrollViewDelegate,
SlideViewDelegate>{
SliderView *thumbGalleryView;//delegate and reference are set in XCode
}
ViewController.m
#import "ViewController.h"
#implementation ViewController
-(void)didTapImageData:(NSMutableArray*) imageData atIndex:(int)index{
NSLog(#"NOT WORKING HERE");
}
So ViewController never receives a call at the method above. The thumbGalleryView is linked to ViewController and delegate is set to ViewController too. SlideView's handleTap is printing message fine but [[self slideViewDelegate] didTapImageData: imageData atIndex:0 ]; is ignored. Why?
You have set the delegate which is ivar of scroll view.
You have to set the slideViewDelegate to ViewController
Edited
Add IBOutlet
IBOutlet __weak id<SlideViewDelegate> slideViewDelegate;
Then from your xib connect slideViewDelegate to ViewController
Also remember to change the class of scroll view to SliderView
Added Image for clarity
Double-check where you set your delegate - if your delegate is nil at the time -handleTap: calls your -didTapImageData:atIndex: method, nothing will happen.

How to call ViewController's method to show 2nd View?

//
// MyGameViewController.h
//
#import < UIKit/UIKit.h >
#import "SecondViewController.h"
#interface MyGameViewController : UIViewController {
IBOutlet SecondViewController *secondViewController;
}
-(IBAction)goToSecondView;
#end
//
// MyGameViewController.m
//
#import "MyGameViewController.h"
#implementation MyGameViewController
-(IBAction)goToSecondView{
[self presentModalViewController:secondViewController animated:YES];
}
//
// MyGameView.h
//
#import < UIKit/UIKit.h >
#import "Sprite.h"
#interface MyGameView : UIView {…}
Currently I have implemented a button on the MyGameView.xib to invoke the secondViewController view and it works. But I want the secondViewController get invoked by programming inside MyGameView.m when there is interruption, not by pressing a button. Therefore, I think there are 2 approaches:
a) Either make the goToSecondView method available to MyGameView.m
b) Implement all the code in MyGameViewController.h and MyGameViewController.m to MyGameView.m.
Issues:
1) When tried to make a) happen, I have to make goToSecondView method starting with (void), not (IBAction). But then how to invoke it in MyGameView.m?
2) I tried to do b) and implemented all code to MyGameView.m. But presentModalViewController is a method of ViewController and does not work in UIView. So what is the solution?
As you stated, you can't call presentModalViewController in a UIView class. This seems like a great opportunity to use a delegate. You could do something along the lines of:
In MyGameView.h
#protocol MyGameViewDelegate
- (void)showSecondView;
#end
#interface MyGameView {
}
#property (nonatomic, assign) id <MyGameViewDelegate> delegate;
...
#end
In MyGameView.m, when you need to show the second view:
[self.delegate showSecondView];
In MyGameViewController.h:
#import "MyGameView.h"
#interface MyGameViewController : UIViewController <MyGameViewDelegate> {
...
In MyGameViewController.m:
#pragma mark MyGameViewDelegate methods
- (void)showSecondView {
[self goToSecondView];
}
Note that you'll also need to set MyGameViewController to be the delegate of MyGameView. You could do that in Interface Builder, or in code, depending on where you create the two objects.
To do it in code, for example in the MyGameViewController.h viewDidLoad method:
myGameView.delegate = self;