Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Controlling One View From Another. How?

Hi there, I have a problem that i find very frustrating... I´m currently working on a application using tab bars. I have made it so when you press the tab ar item you are currently in a actionsheet will pop up, giving you options to choose from. What i want is to be able to control a webview in the current tab with the buttons from the actionsheet. So lets say i want to set the url, i would have a button for that etc...

I have read about different techniques of passing data between viewcontrollers and such, but all of them seems to only be how to pass the value of one UITextfield in viewcontroller1 to the UILabel in viewcontroller2. I´m really confused about how to do this.


This is how i´ve done it this far.


I have a subclass of UITabBarController set up with the name TabController. In my TabController.h I have done following.


//TabController.h


#import <UIKit/UIKit.h>

#import "viewcontroller1.h>


@interface TabController : UITabBarController


{

viewcontroller1 *theViewController1 ;

}


@property (nonatomic, retain) viewcontroller1 *theViewController1;






Then i do the following in my TabController.m



//TabController.m


#import <TabController.h>



Here i try to control the webview by refering to the webview in theViewController1 in the method for clickedbuttonatindex like so:


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

NSLog(@"buttonWithIndex: %d", buttonIndex);

if ((actionSheet.tag == 3) && (buttonIndex == 0))

{

[theViewController1.webView reload];

NSLog(@"Logging When The Method Is Called");


}



I´m able to see that the method is called correctly...


But nothing happens with my webview. I´ve been all around the web, trying to find a solution to this.. But it seems like i cannot find any examples that shows how to control a view from another. I´ve only found the techniques for how to pass data, like strings and such...


Please someone out there, help me.


I would really appreciate some well described code examples of how to do this, as im not that expericenced in programming.


Cheers 🙂






Message was edited by: Klafstad

iPhone 4S, iOS 5.1

Posted on Jun 1, 2012 7:12 AM

Reply
6 replies

Jun 1, 2012 7:44 PM in response to Klafstad

I did a quick sample with a vanilla project created from the Tabbed Application template. I added a UIWebView to FirstViewController. This code is in MainTabBarController. The action sheet is only shown when the user touches the tab bar item to select FirstViewController when that tab is already selected.


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

{

if ((viewController == tabBarController.selectedViewController) &&

([viewController isKindOfClass:FirstViewController.class])) {

[[[UIActionSheetalloc] initWithTitle:nildelegate:selfcancelButtonTitle:@"Cancel"

destructiveButtonTitle:nilotherButtonTitles:@"Refresh", nil]

showFromTabBar:tabBarController.tabBar];

}

returnYES;

}


- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

{

if (buttonIndex == 0)

[((FirstViewController *)self.selectedViewController).webViewreload];

}


If you have multiple action sheets and only one UIActionSheetDelegate, be careful handling didDismissWithButtonIndex (or clickedButtonAtIndex) to make sure you don't incorrectly cast selectedViewController.

Jun 9, 2012 3:35 PM in response to Llessur999

Hmm...


So i´ve come up with another problem now... I´m trying to make is so whenever you press one of the tabbaritems a sound will play. But this will only happen if the "Sound" switch is turned on, which is a UISwitch in my settingsviewcontroller located in one of the tab bars.


So my question would be, how could a call a method of this type cross viewcontrollers?


This is what i´ve tried.


settings.m


- (void)soundPlay

{

if (soundOnOff.on) {

CFBundleRef mainBundle = CFBundleGetMainBundle();

CFURLRef soundFileURLRef;

soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"ButtonPressed", CFSTR ("mp3"), NULL);

UInt32 soundID;

AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);

AudioServicesPlaySystemSound(soundID);

}

}



Here "soundOnOff" is declared as a property of UISwitch and connected to the switch with a IBOutlet in the interface builder.


This is how i´ve tried in my TabController.m which is a subclass of UITabBarController.

I have played around with it in a lot of ways, but I just dont see how to make it... And the more i try, the more confused i get... 😝



tabcontroller.m


[((Settings *)self.selectedViewController) soundPlay];



I would really appreciate if you could give me a code example, and explain how and why it is done, so i could get a better understanding.


Cheers

Jun 10, 2012 10:25 AM in response to Klafstad

Your settings should be persisted somewhere other than the settings view controller. Settings are typically persisted using NSUserDefaults.standardUserDefaults. The soundOnOff UISwitch on the settings view controller should just update the setting, either when the on/off is switched or when the settings view controller is dismissed.


[NSUserDefaults.standardUserDefaultssetBool:shouldPlaySound.on forKey:@&quot;shouldPlaySound&quot;];



Features within the app that depend on settings should get the setting value from NUserDefaults.


if ([NSUserDefaults.standardUserDefaultsboolForKey:@&quot;shouldPlaySound&quot;]) {

// play the sound

}


Your method soundPlay would normally be in the UITabBarController, not on the settings view controller. [BTW, consider changing the method name to begin with the verb, playSound.]

Controlling One View From Another. How?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.