I am trying to make my own delegate method for this I declare 'Mydelegate' protocol in forDelegate.h file.
#protocol Mydelegate <NSObject>
-(void)show:(NSString *)msg;
#end
then in my first viewController I make the property for this.I declare a method clickMe which is call on button press,that button is present in my firstviewcontroller .xib file.
#class SecondViewController;
#interface ViewController : UIViewController
{
id <Mydelegate> delegate1;
SecondViewController *secondController;
}
#property (assign) id <Mydelegate> delegate1;
-(IBAction)clickMe:(id)sender;
#end
and then after I adopt this protocol in my second view controller and define the method of this protocol
SecondViewController.h file
#class ViewController;
#interface SecondViewController : UIViewController <Mydelegate>
{
ViewController *viewController;
}
SecondViewController.m file
-(void)show:(NSString *)msg
{
NSLog(#"in delegate mathod");
}
-(void)viewDidLoad
{
viewController =[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
viewController.delegate1=self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Program is running but Mydelegate method 'show' is not call.
This will be a useful answer for your question.
In the implementation of ViewController, you should call the delegate's method..
-(IBAction)clickMe:(id)sender
{
[self.delegate1 show:#"Your String Param"];
}
Create one button on second view controller xib and connect method to that button then after click that button check your console.
#class ViewController;
#interface SecondViewController : UIViewController <Mydelegate>
{
ViewController *viewController;
}
-(IBAction)showmsg;
SecondViewController.m file
-(void)show:(NSString *)msg
{
NSLog(#"in delegate mathod");
}
-(IBAction)showmsg
{
[self show:];
}
-(void)viewDidLoad
{
viewController =[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
viewController.delegate1=self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
#protocol CPUPerformanceDelegate <NSObject>
-(void)getUsedCpuOperations:(float)percent;
-(void)getKernelCpuOperations:(float)percent;
-(void)getIdleCpuOperations:(float)percent;
-(void)getUserCpuOperations:(float)percent;
-(void)getNiceCpuOperations:(float)percent;
#end
#interface CPUPerformance : NSObject{
processor_info_array_t cpuInfo, prevCpuInfo;
mach_msg_type_number_t numCpuInfo, numPrevCpuInfo;
unsigned numCPUs;
NSLock *CPUUsageLock;
}
#property(nonatomic,assign)id<CPUPerformanceDelegate>delegate;
#property(nonatomic,retain)NSTimer *updateTimer;
#end
Then
#import "CPUPerformance.h"
#implementation CPUPerformance
#synthesize delegate,updateTimer;
- (void)updateInfo
{
idlepercent = ((idle/total)*100);
userpercent = (user/total)*100;
syspercent = (sys/total)*100;
nicepercent = (nice/total)*100;
inUsepercent = (inUse/total)*100;
[delegate getIdleCpuOperations:idlepercent];
[delegate getKernelCpuOperations:syspercent];
[delegate getNiceCpuOperations:nicepercent];
[delegate getUsedCpuOperations:inUsepercent];
[delegate getUserCpuOperations:userpercent];
}
and finally
#import "CPUPerformance.h"
#interface SpecProjectFirstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CPUPerformanceDelegate>{
//Ivars
NSMutableArray *processArray;
//User Interface Object
UILabel *cpuUsage;
UILabel *cpuIdle;
UILabel *cpuUser;
UILabel *cpuNice;
UILabel *cpuKernel;
IBOutlet UITableView *tableViews;
CPUPerformance *cpuObject;
}
=================
#import "SpecProjectFirstViewController.h"
#implementation SpecProjectFirstViewController
-(void)getIdleCpuOperations:(float)percent{
[cpuIdle setText:nil];
[cpuIdle setText:[NSString stringWithFormat:#"Idle :%.0f %%",percent]];
[cpuIdle setTextAlignment:UITextAlignmentCenter];
}
-(void)getKernelCpuOperations:(float)percent{
[cpuKernel setText:nil];
[cpuKernel setText:[NSString stringWithFormat:#"Kernel :%.0f %%",percent]];
[cpuKernel setTextAlignment:UITextAlignmentCenter];
}
-(void)getNiceCpuOperations:(float)percent{
[cpuNice setText:nil];
[cpuNice setText:[NSString stringWithFormat:#"Nice :%.0f %%",percent]];
[cpuNice setTextAlignment:UITextAlignmentCenter];
}
-(void)getUsedCpuOperations:(float)percent{
[cpuUsage setText:nil];
[cpuUsage setText:[NSString stringWithFormat:#"Used :%.0f %%",percent]];
[cpuUsage setTextAlignment:UITextAlignmentCenter];
}
-(void)getUserCpuOperations:(float)percent{
[cpuUser setText:nil];
[cpuUser setText:[NSString stringWithFormat:#"User :%.0f %%",percent]];
[cpuUser setTextAlignment:UITextAlignmentCenter];
}
-(void)viewDidAppear:(BOOL)animated{
cpuObject = [[CPUPerformance alloc]init];
cpuObject.delegate = self;
[self creatObjectsOnView];
[super viewDidAppear:animated];
}
Related
Here is my code.
#interface MasterViewController : UIViewController{
}
-(void) initCallService;
#end
#implementation MasterViewController
-(void) initCallService{
}
#end
other class which is sub class of above
#interface DetailViewController : MasterViewController{
IBOutlet UIButton *btn;
}
#end
#implementation DetailViewController
-(void) btntitle_changed{
[btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal];
}
#end
I need whenever the title changes in btntitle_changed method of DetailViewController class to send it to MasterViewController's initCallService method. I don't know how can i achieve this with the class hierarchy. Can anyone guide me for this ? Valid answer will be appreciated.
You need a property in MasterViewController say NSString *buttonTitle
-(void) btntitle_changed{
[btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal];
[super setButtonTitle:btn.title];
}
Just change your -(void)initCallService to -(void)initCallServiceWithButtonTitle:(NSSTring*)title.
Then change btntitle_changed function as follows:
-(void) btntitle_changed{
[btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal];
[super initCallServiceWithButtonTitle:btn.title];
}
-(void)initCallServiceWithButtonTitle:(NSString*)title{
//do whatever you want with 'title'
}
It is possible by setting a Protocol
in your DetailViewController.h
#protocol TitleChange <NSObject>
-(void)initCallService:(NSString *)title;
#end
and set a property as
#property(nonatomic,assign) id <TitleChange> delegate;
In DetailViewController.m
-(void) btntitle_changed
{
[btn setTitle:[self.arr objectAtIndex:recordIndex] forState:UIControlStateNormal];
[self.delegate initCallservice: btn.title]
}
In your MasterViewContoller.h add TitleChange delegate as
#interface MasterViewController : UIViewController<TitleChange>{
}
In you MasterViewContoller.m while initiating DetailViewController
DetailViewController *vc =[[DetailViewController alloc] :#"DetailViewController" bundle:nil];
vc.delegate=self
[self presentViewController:vc animated:YES completion:nil];
this is how you Protocol and delegates which is widely used for passing data and just go through the basics of it
I'm trying to wire up the delegate in KGModal to MainView, but it doesnt work. Basically I show SecondViewController in KGModal (Github), and when dismissing the KGModal view i want the MainView to know that through the delegate. Any ideas? (sorry for bad explanation). Can't get it to work.
KGModal.h
#class KGModal;
#protocol KGModalDelegate <NSObject>
- (void)modalControllerDidFinish:(KGModal *)controller;
#end
#interface KGModal : NSObject {
UIButton *dismissButton;
}
#property (weak, nonatomic) id <KGModalDelegate> delegate;
KGModal.m
-(void)dismissButtonPressed:(id)sender {
[self.delegate modalControllerDidFinish:self];
[self hideAnimated:self.animateWhenDismissed];
}
MainView.h
#interface MainView : UIViewController <KGModalDelegate>
#property(weak) id<KGModalDelegate> delegate;
MainView.m
- (void)modalControllerDidFinish:(KGModal *)controller{
NSLog(#"Dismissed.");
}
-(IBAction)modalShowing {
SecondViewController *view2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
KGModal *kg = [[KGModal alloc] init];
kg.delegate = self;
[[KGModal sharedInstance] showWithContentView:view2.view andAnimated:YES];
}
Problem solved. I set the delegate to different instances of KGModal.
KGModal *kgm = [KGModal sharedInstance];
kgm.delegates = self;
[kgm showWithContentView:view2.view andAnimated:YES];
Thanks to Hot Licks!
In my iPhone app, I have two UIViewcontroler class firstView and secondView.
From the firstView, I am presenting secondView using presentModalViewController:animated: method.
Problem is that when I am dismissing secondView, I want to hide button in firstView.
Although It does execute the code in firstView [button setHidden:YES];, but still it does not hide the button.
What could be wrong?
Hope you have declared property and synthesized the IBOutlet button.
Make an object of FirstViewController in SecondViewController.h and property and synthesize it.
SecondViewController.h
#interface SecondViewController {
.
.
FirstViewController *firstView;
.
.
}
#property (nonatomic,strong) FirstViewController *firstView;
#end
SecondViewController.m
#implementation SecondViewController
.
.
#synthesize firstView;
.
.
#end
Now when you present a modal view from firstView
FirstViewController.m
-(IBAction)presentModalView {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondView.firstView = self;
[self presentModalViewController:secondView animated:YES];
}
Now in SecondViewController where you dismiss SecondViewController just add this code.
SecondViewController.m
-(IBAction)dismissModalView {
[self.firstView.button setHidden:YES];
[self dismissModalViewControllerAnimated:YES];
}
EDIT:
Refer to this link:
#protocol implementation in #interface in Objective-C
EDIT-2: With Protocol implementation
SecondViewController.h
#protocol SecondViewControllerDelegate <NSObject>
#required
- (void)hideButton;
#end
#interface SecondViewController {
.
.
id <SecondViewControllerDelegate> delegate;
.
.
}
#property (retain) id delegate;
#end
SecondViewController.m
#implementation SecondViewController
.
.
#synthesize delegate;
.
.
#end
Now when you present a modal view from firstView
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController<SecondViewControllerDelegate>
{
.
.
.
.
}
.
.
#end
FirstViewController.m
-(IBAction)presentModalView {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondView.delegate = self;
[self presentModalViewController:secondView animated:YES];
}
#pragma mark - SecondViewController Delegate
- (void)hideButton
{
[self.button setHidden:YES]; //Here button is UIButton you want to hide when second view is dismissed.
}
Now in SecondViewController where you dismiss SecondViewController just add this code.
SecondViewController.m
-(IBAction)dismissModalView {
[delegate hideButton];
[self dismissModalViewControllerAnimated:YES];
}
Let me know if you need more help on this.
Hope this helps.
I would hide the button on the viewDidDissapear method, so when users calls the model the button hides. But this will only work if you cannot show other viewControllers from your firstViewController.
I hope it helps!!
I created two controller, FirstViewController and SecondViewController.
And one button in each controller.
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController{
BOOL firstTime;// to hide button
}
#property (retain, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)buttonClicked:(id)sender;
#end
FirstViewController.m
-(void)viewWillAppear:(BOOL)animated{
if (firstTime) {
[myButton setHidden:TRUE];
}
}
- (IBAction)buttonClicked:(id)sender {
firstTime = TRUE;
SecondViewController *secondController = [[SecondViewController alloc]init];
[self presentModalViewController:secondController animated:TRUE];
[secondController release];
}
SecondViewController.h
#interface SecondViewController : UIViewController
#property (retain, nonatomic) IBOutlet UIButton *secondButton;
- (IBAction)secondButtonClicked:(id)sender;
- (IBAction)secondButtonClicked:(id)sender;
#end
SecondViewController.m
- (IBAction)secondButtonClicked:(id)sender {
[self dismissModalViewControllerAnimated:TRUE];
}
This worked for me. Please try it out.
I am trying change an UIImageView from other viewControllers, but I don't know why image does not change !! here is my code :
#import "ViewController.h"
#class ViewController;
#interface CoverGallery : UIViewController {
ViewController *mainViewController;
}
#property (nonatomic,retain) ViewController *mainViewController;
- (IBAction)img1;
.m
- (IBAction)img1 {
mainViewController.coverArt.image = [UIImage imageNamed:#"coverDefault.png"];
[self dismissModalViewControllerAnimated:YES];
}
There is an UIImageView called coverArt in MainViewController , thanks
Here is my button action which switch between views :
ViewController (My First view)
.h
#interface ViewController : UIViewController {
ViewController *mainViewController;
}
#property (nonatomic, retain) ViewController *mainViewController;
.m :
#synthesize mainViewController;
- (void) CoverGallery {
CoverGallery *gallery = [[CoverGallery alloc] initWithNibName:#"CoverGallery" bundle:nil];
gallery.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:gallery animated:YES];
//here is the problem, compiler gives me the mainViewController is not the property of CoverGallery
gallery. mainViewController = self;
[gallery release];
}
How are you initializing the property mainViewController? You need set it as the MainViewController by setting its property when you present the modal view.
I imagine your doing this in the MasterViewController:
CoverGallery *gallery = [[CoverGallery alloc]initWithNibName:#"CoverGallery" bundle:nil];
gallery.masterViewContrller = self; //Set it here
[self presentModalViewController:gallery animated:YES];
I fixed a typo..
and this:
ViewController *mainViewController;
should be:
MainViewController *mainViewController;
but you need to Import it in the same header file.
i have 2 views
i m sending text in a button on 1st view to label on second view....
//////textfieldtolabelViewController.h
#import <UIKit/UIKit.h>
#import "seconview.h"
#interface textfieldtolabelViewController : UIViewController {
IBOutlet seconview *sec;
//IBOutlet UITextField *t1;
}
//#property(nonatomic, retain)IBOutlet UITextField *t1;
-(void)buttonclick:(id)sender;
#end
and its .m file is this
#import "textfieldtolabelViewController.h"
#implementation textfieldtolabelViewController
-(void)buttonclick:(id)sender
{
NSString *s = [sender titleForState:UIControlStateNormal];
//sec.ss = s;
[sec settext:s];
[self presentModalViewController:sec animated:YES];
}
- (void)dealloc {
[super dealloc];
}
#end
now there is second view naming seconview
.h file
#import <UIKit/UIKit.h>
#interface seconview : UIViewController {
IBOutlet UILabel *l1;
}
#property(nonatomic, retain)IBOutlet UILabel *l1;
-(void)settext:(NSString *)ss;
#end
and its .m file is ....
#import "seconview.h"
#implementation seconview
#synthesize l1;
//#synthesize ss;
-(void)settext:(NSString *)ss
{
l1.text=ss;
}
- (void)dealloc {
[super dealloc];
}
#end
this program did not show any error and runs fine but problem is that the text of button in 1st view does not appear in label on second view
but i made all connection perfectly.......
Most probably you didn't connect l1 to the actual label in the Interface Builder. Verify with the debugger, that l1 is not nil in your settext method
You can include the view controller containing the label in the view controller where you want it to change, and access it by allocating a copy of it.
For example, use something like this in textfieldtolabelViewController.m
#import "seconview.h"
-(void)buttonclick:(id)sender
{
NSString *s = [sender titleForState:UIControlStateNormal];
seconview *viewcontroller = [[seconview alloc] initWithNibName:#"seconview" bundle:nil];
[[viewcontroller l1] setText:s];
[viewcontroller release];
[self presentModalViewController:sec animated:YES];
}
the [self presentModalViewController:sec animated:YES]; should be above the setText
try it
-(void)buttonclick:(id)sender
{
NSString *s = [sender titleForState:UIControlStateNormal];
//sec.ss = s;
[self presentModalViewController:sec animated:YES];
[sec settext:s];
}
hope it will work....