so i am using presentmodalviewcontroller to change the active views in an ipad app. however when i try to change it using the statement [self presentModalViewController:createCharacter animated:NO]; in an ibaction that is triggered by a button. however i get an error saying expected expression before 'createCharacter'. createCharacter is a custom view controller that i have created... does anyone know what i am doing wrong? if you need any more relevant code just let me know,thanks
additional relevant code:
#import "createCharacter.h";
-(IBAction) buildCharacter{
[self presentModalViewController:createCharacter animated:NO];
}
createCharacter.h :
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface createCharacter : UIViewController {
IBOutlet UIView *view;
}
#end
I'd love to see some code to look at, and without it maybe this advice will be wrong, BUT...in my experience I've always used IBActions with a single argument, and that argument has always been the sender, so something like tying a button press to `
-(IBAction) presentNewController:(id)sender`
where sender is the button that was pressed.
If you use a method like that to detect the button press from IB, then in code what you would want is something like:
// In your current view controller, the target where you wired up the button
-(IBAction) presentNewController:(id)sender
{
if([sender isEqual:<whatever button you expect>])
{
CustomController *con = [[[CustomController alloc] init] autorelease];
[selfpresentModalViewController:con animated:YES];
}
}
You need to allocate and initialise createCharacter before you can push it into the view.
Assuming createCharacter is a view controller:
createCharacter *customView = [[createCharacter alloc] initWithNibName:yourNibNameORnil bundle:yourBundleNameORnil];
[self presentModalViewController:customView animated:YES];
[customView release];
It appears that you are sending a class to presentModalViewController:animated:. You need to initialize the class as Rog showed. As for MahatmaManic's answer, I have no idea why that was getting rid of your error. He is correct for OS X, but the argument is not required for iOS.
There are a few reasons you could still be getting the error after following Rog's example:
You have a variable named createCharacter, which means it was already initialized. In this case, change the name of your class to CreateCharacter. Classes are usually capitalized, and this would ensure that it is not confused with a variable.
The createCharacter header file is not properly imported. If you change the name of the file in XCode, it does not actually change the name of the file. If you try importing the file using the new name, it will not work.
Your initialization code wasn't right. In Rog's example, he used the default argument names. You should change yourNibNameORnil to the name of your NIB file, or nil if you are not using a NIB. You should also change yourBundleNameORnil to nil, assuming the NIB is located in your application's bundle. Here is an example assuming the NIB is in the application and named createCharacter.xib:
createCharacter *customView = [[createCharacter alloc] initWithNibName:#"createCharacter" bundle:nil];
Related
I am using the option to play some media, if I link it to an IBAction on ViewController.h it will play fine.
The problem occurs, when I try to call that, from another ViewController, for example;
ViewController *myViewController = [[ViewController alloc] init];
[myViewController showVideos];
This is called from SecondViewController and refers to the code in ViewController.m
-(void)showVideos {
[[ApplifierImpact sharedInstance] showImpact];
}
It works using it if I am viewing it on the ViewController, but the call using the
-(void)showVideos {
[[ApplifierImpact sharedInstance] showImpact];
}
Throws the error about window hierarchy when calling it from the SecondViewController.m file
Now, in the SecondViewController.h file, the only reference to ViewController, is a simple import of the .h file, should I be initialising it or giving it a property in there also?
This: ViewController *myViewController = [[ViewController alloc] init]; creates a new ViewController object. You then don't present it (i.e. don't give it access to the screen) but you ask it to showVideos.
I suspect what you really want is to get a reference to an existing ViewController. When you create your SecondViewController give it a reference to the first one to act as a delegate.
I am working with the library KYCircleMenu. You can find it over here. I am also working with storyboards. I made a Class MenuViewController that is derived from KYCircleMenu
#interface MenuViewController : KYCircleMenu
Next I have implemented my initWithCoder like this.
- (id)initWithCoder:(NSCoder*)aDecoder
{
NSLog(#"called");
if(self = [self initWithButtonCount:kKYCCircleMenuButtonsCount
menuSize:kKYCircleMenuSize
buttonSize:kKYCircleMenuButtonSize
buttonImageNameFormat:kKYICircleMenuButtonImageNameFormat
centerButtonSize:kKYCircleMenuCenterButtonSize
centerButtonImageName:kKYICircleMenuCenterButton
centerButtonBackgroundImageName:kKYICircleMenuCenterButtonBackground])
{
}
return self;
}
And finally I have implemented a method from the KyCicrleMenu RunButtonActions. This method tells me what button is pressed in the menu. So in this method I am trying to do a segue to another viewcontroller. I am doing it like this.
NSLog(#"tag is %d",[sender tag]);
[self performSegueWithIdentifier:#"showNews" sender:self];
(The log gives me the button tag from the button that is pressed).
For some reason or another I keep getting this error.
Receiver (<MenuViewController: 0x1cd7cf50>) has no segue with identifier 'showNews''
Here is a screenshot from my storyboard.
Can anybody help me with this annoying problem?
Kind regards
- (id)initWithCoder:(NSCoder*)aDecoder
{
NSLog(#"called");
if(self = [self initWithButtonCount:kKYCCircleMenuButtonsCount
menuSize:kKYCircleMenuSize
buttonSize:kKYCircleMenuButtonSize
buttonImageNameFormat:kKYICircleMenuButtonImageNameFormat
centerButtonSize:kKYCircleMenuCenterButtonSize
centerButtonImageName:kKYICircleMenuCenterButton
centerButtonBackgroundImageName:kKYICircleMenuCenterButtonBackground])
{
}
return self;
}
Here, you are doing nothing with the aDecoder object - this contains all of the information from the storyboard (including the segue). Instead you are creating a brand new object, ignoring anything you have set up in the storyboard.
I've had a quick look at the repository and it doesn't seem to be tailored towards use in a storyboard - it implements its own loadView method, it has a designated initialiser and so on. You'd have to play around with it to set those properties after calling [super initWithCoder:aDecoder];, perhaps by pulling out the setup code from the designated initialiser and putting it into a separate method.
I've implemented two uiviewcontrollers. I have some text fields in one on a uiview and 1 button on it. What I want is that when I fill text fields and click on the button, these values show on another uiview controller. I am facing a problem when I fill the text fields and click on the button, another uiview on shows on the iPhone screen but the text field values are not shown on it. I also import nextview.h file in my viewcontroller.m file. Below is some of my code:
-(IBAction)save:(id)sender
{
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil];
[[NView name] setText:name.text];
[[NView fathername] setText:father.text];
[[NView country] setText:countryselected.text];
[[NView gender] setText:genderselected.text];
[[NView dob] setText:dateselected.text];
[[NView username] setText:username.text];
[[NView password] setText:password.text];
[[NView email] setText:email.text];
[self presentModalViewController:NView animated:YES];
}
In the nextview class, I also define #property of uilabels in the .h file and I also #synthesize, initialize and release these labels in the nextview.m file and linked these variables to all labels. The problem is still there. What can I do to avoid this problem?
See this line of code:
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil];
You use a nil as nib name to create your next view. Have you overwritten initWithNibName of the nextview to give a nib name?
do you init you property your member(such as name) when call
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil]; if you init them in viewdidiload or other function about view,then they will be initialized after
you call [self presentModalViewController:NView animated:YES];
There i so many method through which you can pass data from one view to another. in this Tutorial there is brief discription about this method Which you Want Check it Tutorial Source Code
See apples documentation for initWithNibName
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
The nib is not loaded until its view is accessed. For additional initialisation use viewDidLoad.
Meaning, the outlets are just not set directly after initWithNibName.
So, one of the things you could do is add a reference to the first viewController as a property in nView and in viewDidLoad of nView you set the labels according to the first VCs labels.
On a side note: your naming convention is wrong. Use Uppercase for classes and lowercase for instances of that class ie it should be
Nextview *nView;
(I've just learn objective-c for a short time. I still don't know how to generate UI elements through writting code now.)
There is one button in .xib.
There is one method in .m.
I want to link these two things by ctrl-click "Touch up Inside" and "File's owner". However, no method shows up when I move the mouse cursor onto File's Owner's icon.
The situation is like this:
http://www.badongo.com/pic/13577347
the .h file:
#import <UIKit/UIKit.h>
#interface ChangeViewController : UIViewController
{
}
-(IBAction)goToSecondView:(id)sender;
-(IBAction)goToCalculatorView:(id)sender;
-(IBAction)test:(id)sender;
#end
the .m file:
#import "ChangeViewController.h"
#import "SecondViewController.h"
#import "CalculatorViewController.h"
#implementation ChangeViewController
-(IBAction)goToSecondView:(id)sender
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}
-(IBAction)goToCalculatorView:(id)sender
{
CalculatorViewController *calculatorViewController =[[CalculatorViewController alloc] initWithNibName:#"CalculatorViewController" bundle:nil];
[self.navigationController pushViewController:calculatorViewController animated:YES];
[calculatorViewController release];
}
Is there anything wrong? Thanks.
Also make sure your File's Owner class is of type 'ChangeViewController'. For doing so, select the File's Owner, Go to the inspector (press command + 4), and set its class to 'ChangeViewController' if not already set.
Please confirm the below points:
1) Have you defined method in .h file?
2) Have you defined method with -(IBAction)
Hope you will be able to bind it.
Cheers.
Double click on file owner's than you will see list of methods you have defined. Connect than method to you control by drag and drop. Once you will attach that method to your button, all the events that can be fired will be displayed. Select TouchUpInside...
link
I'm trying to figure out how I can call a function from another one of my classes. I'm using a RootViewController to setup one of my views as lets say AnotherViewController
So in my AnotherViewController im going to add in on the .h file
#class RootViewController
And in the .m file im going to import the View
#import "RootViewController.h"
I have a function called:
-(void)toggleView {
//do something }
And then in my AnotherViewController I have a button assigned out as:
-(void)buttonAction {
//}
In the buttonAction I would like to be able to call the function toggleView in my RootViewController.
Can someone clarify on how I do this.
I've tried adding this is my buttonAction:
RootViewController * returnRootObject = [[RootViewController alloc] init];
[returnRootObject toggleView];
But I dont think that's right.
You'll want to create a delegate variable in your AnotherViewController, and when you initialize it from RootViewController, set the instance of RootViewController as AnotherViewController's delegate.
To do this, add an instance variable to AnotherViewController: "id delegate;". Then, add two methods to AnotherViewController:
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
Finally, in RootViewController, wherever AnotherViewController is initialized, do
[anotherViewControllerInstance setDelegate:self];
Then, when you want to execute toggleView, do
[delegate toggleView];
Alternatively, you could make your RootViewController a singleton, but the delegate method is certainly better practice. I also want to note that the method I just told you about was Objective-C 1.0-based. Objective-C 2.0 has some new property things, however when I was learning Obj-C this confused me a lot. I would get 1.0 down pat before looking at properties (this way you'll understand what they do first, they basically just automatically make getters and setters).
I tried out the NSNotificationCentre - Works like a charm - Thanks for your reply. I couldn't get it running but the NS has got it bang on.
[[NSNotificationCenter defaultCenter] postNotificationName:#"switchView" object: nil];