Problems wiring up delegate between views - iphone

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!

Related

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.

Change UIImageView from anotherVIewControllers

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.

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.

Go to another view (UIViewController) from within UIViewController

A newbie Objective C question. Im trying to programmaticly change view from another view. But I only succeed in activating the tab button in the tabbarcontroller.
In my ViewDidLoad I have a condition that if thats not met, load the second view instead.
Is there any kind soul that can help out a Objective-C beginner? I have googled and search stackoverflow for the answer but with no luck.
FirstViewController.h
#interface FirstViewController : UIViewController {
some variables
}
some #properties
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
- (void)viewDidLoad
{
if(condition is met) {
[self.tabBarController setSelectedIndex:1];
}
}
In my AppDelegate.h
#import <UIKit/UIKit.h>
#interface otpAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
AppDelegate.m
#implementation otpAppDelegate
#synthesize window;
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];
return YES;
}
Edit!
I found a solution to my problem.
int selectedindex = 1;
self.tabBarController.selectedIndex = selectedindex;
UIViewController *tempvc = [[self.tabBarController viewControllers] objectAtIndex:selectedindex];
[self.view removeFromSuperview];
[self.view addSubview:tempvc.view];
Did the trick.
Try setting the index for the tabBarController instance of the app delegate like yourAppDelegate.tabBarController
If you are using tabBarController as IBOutlet , there is no need to allocate it in your app delegate. Please remove these code from your appDelegate method (application: didFinishLaunchingWithOptions)..
tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];