how do i get subclass UIButton title in superclass objective c - iphone

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

Related

Issue with UIButton Subclass delegate method

Here is the situation. I have a view controller titled "MyViewController." Within this view controller I have a text editing feature that uses subclassed buttons. The name of the UIButton Subclass is "ColorSwatch"
I have setup delegate/protocol methods in the "ColorSwatch.h" subclass as follow.
// ColorSwatch.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#protocol ColorSwatchDelegate <NSObject>
- (void)fontColor:(UIColor *)color;
#end
#interface ColorSwatch : UIButton {
id <ColorSwatchDelegate> colorSwatchDelegate;
CAGradientLayer *gradient;
UIView *currentView;
UIColor *fontColor;
}
#property (nonatomic, retain) id <ColorSwatchDelegate> colorSwatchDelegate;
#property (nonatomic, retain) CAGradientLayer *gradient;
#property (nonatomic, retain) UIView *currentView;
#property (nonatomic, retain) UIColor *fontColor;
#end
Now in my "ColorSwatch.m" I have:
// ColorSwatch.m
#import "ColorSwatch.h"
#import <QuartzCore/QuartzCore.h>
#import "MyViewController.h"
#implementation ColorSwatch
#synthesize gradient;
#synthesize currentView;
#synthesize colorSwatchDelegate;
#synthesize fontColor;
-(void)setupView{
"Makes the subclassed buttons pretty"
}
-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if((self = [super initWithCoder:aDecoder])){
[self setupView];
MyViewController *mvc = [[MyViewController alloc] initWithNibName:
#"MyViewController" bundle:nil];
self.colorSwatchDelegate = mvc;
}
return self;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self magnify:view];
fontColor = view.backgroundColor;
[self.colorSwatchDelegate fontColor:fontColor];
}
- (void)magnify:(UIView *)view
{
}
- (void)dealloc
{
[currentView release];
[gradient release];
[fontColor release];
[super dealloc];
}
#end
In the "MyViewController.h" I have:
// MyViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ColorSwatch.h"
#interface MyViewController : UIViewController <ColorSwatchDelegate> {
UITextField *photoLabelTextField;
}
#property (nonatomic, retain) IBOutlet UITextField *photoLabelTextField;
#end
In the "MyViewController.m" I have:
- (void)fontColor:(UIColor *)color
{
NSLog(#"Selected Font Color");
[self.photoLabelTextField setTextColor:color];
}
Now the delegate method sort of works, meaning when I tap on a color button the
NSLog(#"Selected Font Color");
message gets fired. But the problem is that I cannot change the
[self.photoLabelTextField setTextColor:color];
property. I have tried numerous ways of changing the property, the only thing that I can do is send NSLogs, anything I try to change a property in the "MyViewController" Class nothing happens.
If anyone could please help me out, I would appreciate it.
Thank you
The problem is that the ColorSwatch is sending delegate messages to a dangling instance of MyViewController that it incorrectly allocated in it's initWithCoder: method.
UIControls shouldn't allocate ViewControllers to be their delegates... it goes the other way around.
Delete these lines...
// in ColorSwatch.m initWithCoder:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:
#"MyViewController" bundle:nil];
self.colorSwatchDelegate = mvc;
Then, in MyViewController.m ...
- (void)viewDidLoad {
ColorSwatch *colorSwatchButton = [[ColorSwatch alloc] buttonWithType:UIButtonTypeCustom];
// or place a ColorSwatch in the xib, on MyViewController's view... But not before you
// you delete lines from initWithCoder, otherwise it's infinite circular allocation
colorSwatchButton.frame = CGRectMake(/* ... */);
colorSwatchButton addTarget:self action:#selector(csButtonPressed:) forControlEvent: UIControlEventTouchUpInside];
// and so on...
// now the important part:
colorSwatchButton.colorSwatchDelegate = self;
// see - the ViewController is in charge of allocation, sets itself up as the delegate
[self.view addSubview:colorSwatchButton];
}
Instead of building the button in code, you can use IB.
Step 1: make the delegate an outlet...
#property (nonatomic, retain) IBOutlet id <ColorSwatchDelegate> colorSwatchDelegate;
Step 2: draw the buttons in IB, and set their class to ColorSwatch.
Then you can skip the code I wrote in viewDidLoad.
Step 3: The newly placed button should now present an outlet in IB. You can drag from that to the MyViewController as you normally do.
There might be a connection problem in your IBOutlet photoLabelTextField, you may have forgotten to connect xib text field with your photoLabelTextField

How to make your own delegate method?

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

Hide Button in another-view from Modal View

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.

Xcode: Does the delegating object HAVE to send a message to the delegate object?

I'm trying to assign SecondViewController as a delegate object of FirstViewController (if I understand correctly). However FirstViewController doesn't send any messages to SecondViewController.
Am I allowed to pretend as though SecondViewController did get a message from FirstViewController and respond to the FirstViewController? (Note: My SecondViewController is in charge of a view that has a button. When I press the button on my SecondViewController's view I want it to tell the FirstViewController to update its view)
FirstViewController.h
#import <UIKit/UIKit.h>
#protocol FirstViewControllerDelegate <NSObject>
#optional
- (void) setAnotherLabel;
#end
#interface FirstViewController : UIViewController {
IBOutlet UILabel *label;
id <FirstViewControllerDelegate> delegate;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
- (void) pretendLabel;
- (void) realLabel;
#end
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize label;
#synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void) setAnotherLabel;
{
label.text =#"Real";
[self.view setNeedsDisplay];
}
- (void) pretendLabel;
{
label.text =#"Pretend";
[self.view setNeedsDisplay];
}
- (void) realLabel;
{
[self setAnotherLabel];
}
- (void)viewDidLoad
{
[super viewDidLoad];
label.text=#"Load";
[self pretendLabel];
}
...
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "FirstViewController.h"
#interface SecondViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, FirstViewControllerDelegate>
{
UIImage *image;
IBOutlet UIImageView *imageView;
}
- (IBAction) sendPressed:(UIButton *)sender;
- (IBAction) cancelPressed:(UIButton *)sender;
#end
SecondViewController.m
#import "SecondViewController.h"
#implementation SecondViewController
- (IBAction) sendPressed:(UIButton *)sender
{
FirstViewController *fvc = [[FirstViewController alloc] init];
[fvc setDelegate:self];
//how do I find out if I'm actually the delegate for FirstViewController at this point?
[fvc realLabel];
self.tabBarController.selectedIndex = 0;//switch over to the first view to see if it worked
}
There are a few issues with this and what appears to be a bit of confusion.
I assume that FirstViewController and SecondViewController are in separate tabs in the tab bar controller.
In the sendPressed: method, you're creating a new instance of FirstViewController - this is not the same FirstViewController that is in your tab bar controller and why calling realLabel has no effect.
The second point is that you appear to misunderstand how delegation works - there is no reason for a delegate in the code you posted.
Good read for getting to grips with delegates: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html
As for a solution to your problem there are a few options:
Post a notification from SecondViewController that FirstViewController is observing (lots of information available on the net regarding notifications).
Get the specific instance of FirstViewController within the self.tabBarController.viewControllers array and call the method from there. Something like...
- (IBAction) sendPressed:(UIButton *)sender
{
for(UIViewController *controller in self.tabBarController.viewControllers)
{
if([controller isKindOfClass:[FirstViewController class]])
{
FirstViewController *firstViewController = (FirstViewController *)controller;
[firstViewController realLabel];
}
}
self.tabBarController.selectedIndex = 0;//switch over to the first view to see if it worked
}
There are more options available than this, but the above will give you a good start into researching the best approach for your need.
Hope this helps.

sending string between views

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....