I am very new to programming, especially iOS, so any answer should be given as if I am a baby with ADHD.
This is what I have in my buttonclick:
ItemCreateViewController *itemCreateViewConrtoller = [[ItemCreateViewController alloc] init];
[[self navigationController] pushViewController:itemCreateViewConrtoller animated:YES];
What am I missing?
There are a lot of reasons why this could be happening among which is your button is not triggering the intended action. Among the reason why a button could not trigger is your XIB button is not connected to the button object defined in your header and method file.
If you look at the .h file look at all your IBOutlet items there should be a circle on the left most column if it is darkened out it is connected other wise it is not, and you need to connect this in your XIB file.
If this is not the problem please display the code where you specify the selector it should be something like this:
[cancel_button addTarget:self action:#selector(Cancel:) forControlEvents:UIControlEventTouchUpInside];
Make sure that the selector is an IBAction function -
-(IBAction)Cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
return;
}
One other thing, any statement after pushViewController will get executed prior to any actual change in the view. The actual View change only happens once control is returned to IOS. This is significant if your push view controller command is within a long conditional process and you fail to code the return / exit properly.
Since you are using a XIB file for your second view, you need to create it as follows -
ItemCreateViewController *itemCreateViewConrtoller = [[ItemCreateViewController alloc] initWithNibName:#"ItemCreateViewController" bundle:nil]; //Ensure you use correct nib file name
Then you can push it
[[self navigationController] pushViewController:itemCreateViewConrtoller animated:YES];
But since you are using a Storyboard you could have just added the new view to your storyboard, linked the button to the new view with a "push" segue and everything would have been done for you.
Related
I have view controller I want to load different web pages on button clicks.
I have single view controller where i can load web page.
Button click events
- (IBAction)btnResortTVTouch:(id)sender {
GlobalWebViewController *globalWebViewController1 = [[GlobalWebViewController alloc] init];
globalWebViewController1.strUrlName = #"http://www.youtube.com/user/xyz";
[self presentViewController:globalWebViewController1 animated:YES completion:nil];
[globalWebViewController1 selectPageLink];
}
- (IBAction)btnPIntrestTouch:(id)sender {
GlobalWebViewController *globalWebViewController = [[GlobalWebViewController alloc] init];
globalWebViewController.strUrlName = #"http://www.pinterest.com/xyz/";
[self presentViewController:globalWebViewController animated:YES completion:nil];
[globalWebViewController selectPageLink];
}
It gives error
2013-12-19 03:00:17.885 RWNewYork[5941:907] Warning: Attempt to present GlobalWebViewController: 0x80c8e90 on FiveViewController: 0x81af490 which is already presenting GlobalWebViewController: 0x80826e0
You probably copied the first button, that is how you created the second one in the storyboard, and now it has 2 separate actions. So for one of your buttons both methods are called. Right-click on them in the storyboard and you can see the actions attached.
Edit:
Maybe you should call the selectPageLink method from the completion block of the presentation.
It's telling you that you've already presented a GlobalWebViewController from your FiveViewController, and you're trying to present another GlobalWebViewController from it as well, which isn't possible. So your button press must be somehow calling both methods, or calling one of them twice.
I've been trying to use the new xcode 5 storyboard paradigm and not create .xib files specifically. I've got two VCs - the first was created by Xcode, the second was created by dropping a UIViewController into InterfaceBuilder.
My issue is that even though I've given my second VC a custom class and imported the headers into the first VC, when I create the second VC programatically it doesn't conform to the design I set in InterfaceBuilder. The first one does.
My code to create the second VC goes something like this (it's inside an ImagePickerControllerDelegate method) :
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
NCPhotoViewController *newController = [[NCPhotoViewController alloc] init];
newController.theImage = chosenImage;
** NEW CODE newController.theImageView.image = newController.theImage;
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:newController animated:YES completion:NULL];
}];
...which seems fine, and does what I want it to - brings up a new view controller. Thing is, it doesn't load with the layout I set in IB. I tried exchanging the the alloc] init] method to initWithNibName:bundle: but realised that I didn't a nib... is there a way I can make this connection in IB without creating a .xib file, or have I misunderstood the new flow of Xcode 5? I'm aware that there's a Storyboard ID attribute in the Identity Inspector, but how do I use that to my advantage? I don't want to do things "the old way" if they're not being put there by default...
TL;DR - I'd like my IB laid-out ViewController to look the way it's intended when called programatically in Xcode 5 default storyboard flow.
Added some code to make the imageview of the new viewcontroller have an image, but it doesn't appear...
EDIT: I had to add self.theImageView.image = theImage; in the viewDidLoad method in my new viewcontroller.h file - but shouldn't it already do that with the code I've written?
If you setup your controller in storyboard you should load it from it. If your code is already in some controller from the same storyboard you can do it the following way:
UIStoryboard *sb = self.storyboard;
NCPhotoViewController * newController = [sb instantiateViewControllerWithIdentifier:#"SomeIdentifier"];
...
You also need to set Storyboard ID for your view controller to "SomeIdentifier" so storyboard will know which controller to instantiate. Here's how it will look in Utilities pane in IB:
in my iPad-app I am trying to present one of my views with a modal formsheet-style.
Here's some code:
-(void)present
{
SecondViewController *modal = [[SecondViewController alloc]init];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
I am using Storyboard, and I have put stuff like a textView and toolbars in the view I'd like to show. I have set the right class in Identity Inspector, and in the class-files I have checked that it's the right view appearing with putting NSLog(#"Right view");
When calling the void present, a view is appearing, but only as a dark-white square. Nothing og my content from Storyboard is in it, I even tried changing the background color of the view and the textView to see if something was just outside the square, but the whole thing stayed white. It feels like it's not using the view I created in storyboard, but I have set it to the correct class, and the NSLog gets printed out when calling it. I have not connected the two views in any way in Storyboard, the SecondViewController is just floating around, so that might be the problem? The button that calls for -(void)present is created programmatically, so I can't ctrl+drag it to the button either.
Why is it showing an empty version of my class?
In the "Identity Inspector" set a "Storyboard ID" for your ViewController, and then present it like this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentModalViewController:modal animated:YES];
}
And if you're using iOS6, presentModalViewController:animated: is deprecated, so use this:
-(void)present
{
SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:#"myStoryboardID"];
modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
[self presentViewController:modal animated:YES completion:nil];
}
Your problem is that you're assuming the program will intrinsically know where to find the, already laid out, view for this controller when that's simply not how storyboards work. The code you list about will create a view controller, but without an associated view it will simply show as a black square.
There's a few ways to solve your dilemma:
Add the modal transition as a segue in the view controller, this would be the simplest way and is what iOS storyboards expect you to do.
Move the view from the storyboard to an external .xib and call the initWithNibName:bundle: method to load this as your view controller's view. This is the best solution if you just want to programmatically load the view.
Load the view from your storyboard programmatically with the instantiateViewControllerWithIdentifier: method, this is probably a bad idea as it goes against the design of storyboards.
I can elaborate on those if you want.
I'm attempting to embed an editable UITextView inside a UIPopoverController, with... strange results. The steps I've taken are:
Create a custom UIViewController class, and create a .xib file with that controller with a UITextView inside it.
When the UI action that should bring up the controller occurs (touch up inside), I instantiate the controller and its view from the .xib file.
I create a new UIPopoverController, with the view controller I just instantiated as the content view.
I present it with presentPopoverFromRect:inView:permittedArrowDirections:animated:
Here's some example code:
- (void)noteButtonPressed:(id)sender {
self.noteview = [[MyTextPopupViewController alloc] initWithNibName:#"MyTextPopupViewController" bundle:nil ];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:self.noteview];
self.popover = popoverController;
self.popover.delegate = self;
[self.popover presentPopoverFromRect:((UIView*)sender).frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Then, inside MyTextPopupViewController, I make sure the the text view is the first responder to bring up the keyboard:
- (void)viewDidAppear:(BOOL)animated {
[self.view becomeFirstResponder];
[super viewDidAppear:animated];
}
And that all works... right until it doesn't. Sometimes, it works perfectly; other times, either immediately or after a few keystrokes, the application crashes by exiting the main event loop (!). No exception is thrown (at least not that the lldb catches), but the application simply stops, both on the simulator and on hardware.
Any thoughts? Has anyone gotten this working successfully, or knows for sure that it does not?
I think the UIPopoverController instance needs to be a property in your code.
How to get actions from another controller without [self presentModalViewController: ololo animated:YES];?
Can I use just
Tutorial *ololo = [[Tutorial alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:ololo.view];
?
Right now with this code I get EXC BAD ACCESS error, when I'm trying to press button on new view.
May it will be easier to create 2 subclasses of UIView with their own XIBs, or may be I can use NavigateController without navigation bar?
PS Yes, I have Tutorial.h, Tutorial.m, Tutorial.xib. In XIB file there are 2 views (portrait and landscape).
Your question is not clear.
try to pass xibf file name as parameter to initWithNibName,ekse just use int method.hope it wont crash
Make sure that your Tutorial object extends UIViewController
#interface Tutorial : UIViewController {
Also make sure that you have a Tutorial.xib file, which has a view, and the outlet from the view it's linked with the one from the viewcontroller.
as a best practice try this :
Tutorial *ololo = [[[Tutorial alloc] initWithNibName:#"Tutorial" bundle:nil] autorelease];
[self.view addSubview:ololo.view];
Also, if you need 2 views in the same view controller, you can just add 2 views in IB, add an outlet to the second one, and add it as a subview of the main one :
[self.view addSubview:secondView]
this way, both of them are managed by the same viewcontroller, and you can add actions in the same view controller.