I have a subview that is linking to a detail subview. See below. This .h is EXACTLY the same in the two header files I am currently using, except for the #interface _ (JohnAdams on one and GeorgeWashington on the other.
#interface JohnAdams : UIViewController {
NSString *selectedCountry;
IBOutlet UIButton *bio;
IBOutlet UIButton *wiki;
}
#property (nonatomic, retain) NSString *selectedCountry;
#property (nonatomic, retain) UIButton *wiki;
#property (nonatomic, retain) UIButton *bio;
-(IBAction)wikiButtonPressed:(id)sender;
-(IBAction)bioButtonPressed:(id)sender;
#end
However, take a look at the two .m's
//George Washington.m
-(IBAction)wikibuttonPressed:(id)sender {
WebView1 *detailvc = [[WebView1 alloc] initWithNibName:#"WebView1" bundle:nil];
[detailvc setTitle:#"Wikipedia"];
[self.navigationController pushViewController:detailvc animated:YES];
[detailvc release];
}
-(IBAction)biobuttonPressed:(id)sender {
WebView2 *detailvc2 = [[WebView2 alloc] initWithNibName:#"WebView2" bundle:nil];
[detailvc2 setTitle:#"The White House"];
[self.navigationController pushViewController:detailvc2 animated:YES];
[detailvc2 release];
}
//JohnAdams.m
-(IBAction)biobuttonPressed:(id)sender {
WebView4 *detailvc4 = [[WebView4 alloc] initWithNibName:#"WebView4" bundle:nil];
[detailvc4 setTitle:#"The White House"];
[self.navigationController pushViewController:detailvc4 animated:YES];
[detailvc4 release];
}
-(IBAction)wikibuttonPressed:(id)sender {
WebView3 *detailvc3 = [[WebView3 alloc] initWithNibName:#"WebView3" bundle:nil];
[detailvc3 setTitle:#"Wikipedia"];
[self.navigationController pushViewController:detailvc3 animated:YES];
[detailvc3 release];
}
In John Adams.m, rather than loading WebView3 and WebView4, it loads WebView1 and WebView2. Additionally, John Adams.m receives a warning that method definition for both -wikiButtonPressed and bioButtonPressed are undefined (but not on GeorgeWashington.m). What am I doing wrong?
Are you using the interface builder?
(Apparently you do because you are declaring IBOutlets)
If so then check the class name of the files owner of JohnAdams.xib.
Related
I would like to pass some variables from my first controller:
float user_distance;
UIImage *user;
float user_battery;
NSString *user_name;
To a second controller.
The connection between the two is made by the function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
in this manner:
UserViewController *secondViewController = [[UserViewController alloc] initWithNibName:#"UserViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
How is it possible to recover the values in from this function?
Thanks in advance!
Add the properties you need to your second controller in your .h file ..
//SecondController.h
#interface SecondController : UIViewController
#property (nonatomic) float *property1;
#property (nonatomic, strong) UIImage *property2;
#property (nonatomic) float *property3;
#property (nonatomic, strong) NSString *property4;
#end
Then import your second controller .h file in your first controller .m file..
And set the properties before pushing the second controller.
//FirstController.m
#import "SecondController.h"
...
...
UserViewController *secondViewController = [[UserViewController alloc] initWithNibName:#"UserViewController" bundle:nil];
secondViewController.property1 = ;//your value here
secondViewController.property2 = ;//your value here
secondViewController.property3 = ;//your value here
secondViewController.property4 = ;//your value here
[self.navigationController pushViewController:secondViewController animated:YES];
I am loading UIViewcontroller from storyboard using instantiateviewcontroller method of UIStoryboard. As soon as this method is executed, viewDidLoad() method is called, even before initWithCoder. This behaviour is different and contradicts lazy loading.
Now, the problem is how to set properties on UIViewcontroller so that I can use them in viewDidLoad method ?
I'm not correctly understand you initial question.
But i think you want something like this
-(void) loadNewVC {
SampleViewController *sample = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"sample"];
NSData *birthData =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://iglesiasSite.com"]];
sample.someName = #"Enrique Iglesias";
sample.someData = birthData;
[[[UIApplication sharedApplication] keyWindow] setRootViewController:sample];
}
in header of SampleViewController.h class
#interface SampleViewController : UIViewController
#property (nonatomic, strong) NSString *someName;
#property (nonatomic, strong) NSData *someData;
#end
SampleViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self processData: self.someData andName: someName];
}
Im building an app that calls different subviews and lay them over in a main view controller (http://imgur.com/p6l9Oac)
when ever the user clicks a button on the bottom part of the screen (lets call it sliding menu!) the view behind it will disappear and new one will show up.
one of the subviews is Settings , which it have some switches to enable/disable some of the buttons.
in the SettingsViewController.
Ive set a protocol:
#protocol SettingsViewControllerDelegate <NSObject>
#required
-(void)hideCountdownView;
-(void)showCountdownView;
#end
and the interface contains:
#interface SettingsHubViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISwitch *enableCountdown;
#property (nonatomic, assign) id <SettingsViewControllerDelegate> delegate;
#property (weak, nonatomic) IBOutlet UIView *mainView;
#end
and in the Implementation:
- (IBAction)switchAction:(id)sender {
if (!self.enableCountdown.on) {
NSLog(#"The view is Hidden");
[_delegate hideCountdownView];
} else if (self.enableCountdown.on) {
NSLog(#"The view is Shown");
[_delegate showCountdownView];
}
}
You can see i used _delegate to use the show and hide functions, I used NSLog to make sure that Im calling the functions correctly.
in the MainViewController
#import "SettingsHubViewController.h"
#interface MainViewController () <SettingsViewControllerDelegate>
#property (nonatomic, strong) SettingsHubViewController * settingsViewController;
and the Implementation
#implementation MainViewController
-(void)showCountdownView {
self.slidingMainMenuViewController.countdownView.hidden = NO;
NSLog(#"Showing Countdown");
}
-(void)hideCountdownView {
self.slidingMainMenuViewController.countdownView.hidden = YES;
NSLog(#"Hiding Countdown");
}
-(void)viewDidLoad {
[super viewDidLoad];
self.settingsViewController.delegate = self;
self.slidingMainMenuViewController.delegate = self;
}
the problem is that the NSLogs above is not being called at all, can any one help me ?
Thanks
UPDATE: Since i have more that 20 different views that needs to be called, i created this method
- (UIView *) getPresentedMenu:(NSString *) menuIdentifer withMenuTag:(int) menuTag withAViewController:(UIViewController*) menuViewController andMenuDelegate:(id) menuDelegate {
menuViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:Nil] instantiateViewControllerWithIdentifier:menuIdentifer];
menuViewController.view.tag = menuTag;
if (self.viewBeingCalledBySwipe == NO) {
menuViewController.view.frame = CGRectMake(0, menuViewController.view.frame.size.height, menuViewController.view.frame.size.width, menuViewController.view.frame.size.height);
} else if (self.isItRightSwipe == YES) {
menuViewController.view.frame = CGRectMake(-menuViewController.view.frame.size.width, 0, menuViewController.view.frame.size.width, menuViewController.view.frame.size.height);
} else if (self.isItRightSwipe == NO) {
menuViewController.view.frame = CGRectMake(menuViewController.view.frame.size.width, 0, menuViewController.view.frame.size.width, menuViewController.view.frame.size.height);
}
[self.view addSubview:menuViewController.view];
[self addChildViewController:menuViewController];
[menuViewController didMoveToParentViewController:self];
UIView *view = menuViewController.view;
return view;
}
So when ever i need a certain view controller, i just call this function
self.childView = [self getPresentedMenu:#"Settings" withMenuTag:SETTINGS_TAG withAViewController:self.settingsViewController andMenuDelegate:self.settingsViewController.delegate];
but this method is not assigning the delegate
If settingsViewController is an IBOutlet, you have to add IBOutlet to it's declaration and connect it using the storyboard or xib.
#property (nonatomic, strong) IBOutlet SettingsHubViewController * settingsViewController;
But if it's not an Outlet, you have to allocate it before set it's delegate.
self.settingsViewController = [[SettingsHubViewController alloc] init];
self.settingsViewController.delegate = self;
You should also verify that your - (IBAction)switchAction:(id)sender IBAction is correctly connected in the storyboard or xib.
When you initialize your SettingsViewController, make sure that you do it using the storyboard method and not [[SettingsViewController alloc] init].
First set a Storyboard ID directly in your storyboard view:
Then use this to settingsViewController:
self.settingsViewController = [[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:NULL] instantiateViewControllerWithIdentifier:#"SettingsViewController"];
Replace MainStoryboard_iPhone with your Storyboard name.
Hope that helps!
// If u are using View controller
SettingsHubViewController *settingVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourStoryBoardIdentifier"];
settingVC.delegate = self;
// If u are using XIB load the NIB after that set the settingVC.delegate = self;
its working for me (I created OverLay view and loaded in main class)
Hoping someone had to solve related issues .. this is driving me nuts :/
My UITableViewController implements a custom delegate method:
.h
#protocol folderDelegate
#required
- (void)folderViewDidSelectPlan:(NSString*)planId;
#end
#interface FolderViewController : UITableViewController
#property (nonatomic, assign) id delegate;
#end
.m
#implementation FolderViewController
#synthesize delegate;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSDictionary *row = [self->resultsPlan objectAtIndex:indexPath.row];
if ([delegate respondsToSelector:#selector(folderViewDidSelectPlan:)]) {
[delegate folderViewDidSelectPlan:[row objectForKey:#"id"]];
}
}
In my iPad's MainView I'm displaying this UITableView via UIPopoverController:
#interface ProjectViewController ()<folderDelegate>
...
- (void) selectPlan:(UIBarButtonItem*)sender
{
if([self->popoverSelectPlanController isPopoverVisible]){
[self->popoverSelectPlanController dismissPopoverAnimated:YES];
}
FolderViewController *folder = [[FolderViewController alloc] initWithStyle:UITableViewStyleGrouped withInstallation:self->_installationId withProjectId:self->_projectId withParentFolderId:#""];
folder.delegate = self;
UINavigationController *folderNavView = [[UINavigationController alloc] initWithRootViewController:folder];
self->popoverSelectPlanController = [[UIPopoverController alloc] initWithContentViewController:folderNavView];
[self->popoverSelectPlanController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
And handling the delegate via:
- (void) folderViewDidSelectPlan:(NSString *)planId
{
NSLog(#"called");
}
However, folderViewDidSelectPlan never get's called - I'm really stuck here, hope anyone has an idea how to solve this.
Thanks a lot!
Try to change declaration of the property to:
#property (assign) id<folderDelegate> delegate;
And also use self.delegate instead of in your UITableViewController.m file every time instead of just delegate.
If you don't have to support iOS4 or less remove synthesise from UITableViewController.m.
I am trying to pass textfield text from one controller to another controller so I can email it. I am following an example, but I must be missing some little details. Basically the goal is to pass data textfield text from enteryournamecontroller controller to option controller's nsstring. The nsstring's string variable is being returned null.
EnterYourNameController.h
#interface EnterYourNameController : UIViewController{
UITextField *textField;
id <EnterYourNameController> delegate;
}
#property (strong, nonatomic) NSString *stringEntered;
#property (strong, nonatomic) UITextField *textField;
#property (strong, nonatomic) id delegate;
EnterYourNameController.m
-(void)button{
stringEntered=textField.text;
((Options *) (self.delegate)).string = stringEntered;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Options *vc = [storyboard instantiateViewControllerWithIdentifier:#"Options"];
[self.navigationController pushViewController:vc animated:YES];
}
options.h
#interface Options : UIViewController <MFMailComposeViewControllerDelegate> {
NSString *string;
}
#property (strong,nonatomic) NSString* string;
#end
option.m
NSString *emailBody = [NSString stringWithFormat:#"%#",string];
In -(void) button method, initialize the Options controller and set the value.
-(void)button{
stringEntered=textField.text;
Options *optViewController = [[Options alloc] init];
optViewController.string = stringEntered;
//call your optViewController here
}
I hope, your string in option VC wouldn't live, when you switch between views. So, you must retain string property like this:
-(void)button
{
Options *optViewController = [[Options alloc] init];
optViewController.string = [[NSString stringWithString:textField.text] retain];
//call your optViewController here
}