Change UIImageView from anotherVIewControllers - iphone

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.

Related

Problems wiring up delegate between views

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!

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.

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

Is that right if I switch View in this way... (IPhone)

I have a MyAppAppDelegate, it contains a window, and a UITabBarController.
#interface MyAppAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
And I have View A, that contain a button to switch to View B. It is the .h file:
#import <UIKit/UIKit.h>
#class MyAppAppDelegate;
#class ViewBController;
#interface ViewAController : UIViewController {
IBOutlet UIView *view;
IBOutlet UIButton *switchToViewBButton;
}
#property (retain, nonatomic) UIView *view;
#property (retain, nonatomic) UIButton *switchToViewBButton;
-(IBAction) startSwitching: (id)sender;
#end
And it is the.m file:
#import "ViewAController.h"
#import "ViewBController.h"
#import "MyAppAppDelegate.h"
#implementation ViewAController
/*skip the default generated codes*/
-(IBAction) startClock: (id)sender{
NSLog(#"Start Switching");
[rootController presentModalViewController:ViewBController animated:YES];
}
Plz notice that the ViewB is not enable to display on UITabBarController, it only appear, when the ViewA button is clicked. Also, I found that the debugger tell me that the rootController is undeclared. but I already import MyAppDelegate to the file. thz a lot... ...
You need to synthesize the rootController instance:
#synthesize rootController;
Then it should work. Put this line of code below the implementation line in the .m file. There is no reason why you should be getting the second error, so try my solution and then tell us what happened.
Also, please try to write in complete sentences. In my experience, if you write well in a forum post, you will gain more respect from people who might help you.
No you need to do something like this:
ViewBController* vc = [[ViewBController alloc] initWithNib: #"ViewBController" mainBundle: nil];
if (vc != nil) {
[rootController presentModalViewController: vc animated:YES];
[vc release];
}
The mistake that you are making is that you are passing presentModalViewController: the class of the ViewBController. Instead it needs an instance.
ViewBController* viewBController = [[[ViewBController alloc] initWithNibName: #"NameOfViewBControllerNibFile" bundle:nil] autorelease];
[self presentModalViewController:viewBController animated:YES];
You can not access rootController from ViewAController, because it is a property of MyAppAppDelegate, not ViewAController. If you want to access the UITabBarController in charge of ViewAController, then inside ViewAController you use self.tabBarController
So if you want the UITabBarController to do the above, change it to
ViewBController* viewBController = [[[ViewBController alloc] initWithNib: #"NameOfViewBControllerNibFile" mainBundle: nil] autorelease];
[self.tabBarController presentModalViewController:viewBController animated:YES];
ViewBController *vc = [[[ViewBController alloc] initWithNib:#"ViewBController"
mainBundle:nil] autorelease];
MyAppDelegate *appDelegate = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.rootController presentModalViewController:vc animated:YES];