I'm attempting to obtain the HTML code from a UITextView and let users view it, live, in a UIWebView in the next segue. Here is what I have so far.
DetailViewController - view with UITextView:
UITextView: codeView
PreviewViewController - view with UIWebView
UIWebView: webView
UIWebView Code: webViewCode
DetailViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([[segue identifier] isEqualToString:#"webViewCode"]) {
PreviewViewController *preview = [segue destinationViewController];
preview.webViewCode = self.codeView.text;
self.preview.webViewCode = [[NSString alloc] initWithString:codeView.text];
[[segue destinationViewController] setWebViewCode:codeView.text];
}
}
PreviewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[webView loadHTMLString:webViewCode baseURL:nil];
}
Everything is connected in the interface builder, including the Storyboard Segue Identifier. As you can see I've tried multiple ways on passing the information through but have only come up with errors. Is there something I'm missing?
This code does not make sense:
if ([[segue identifier] isEqualToString:#"webViewCode"]) {
PreviewViewController *preview = [segue destinationViewController];
preview.webViewCode = self.codeView.text;
self.preview.webViewCode = [[NSString alloc] initWithString:codeView.text];
[[segue destinationViewController] setWebViewCode:codeView.text];
}
You create a local variable preview, of type PreviewViewController. As the other poster pointed out, you need to add a typecast to this to avoid a compiler warning:
PreviewViewController *preview =
(PreviewViewController*) [segue destinationViewController];
The real problem, though, is that you then ignore the local variable you just defined, and try to use a property, self.preview, which is backed by a completely different instance variable, and probably nil. Don't do that. get rid of the "self." bit in this line:
preview.webViewCode = [[NSString alloc] initWithString:codeView.text];
And then delete the line below completely, since it does the same thing as the line before.
Related
I'm Beginner in iOS development. In my FirstViewController I have two textFields and I use the UITextFieldDelegate to make these two equal the same content. In my SecondViewController I have another textField and I need to make it have the same content with the textFields from the FirstViewController.
FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_FirstTextField.delegate = self;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
SecondTextField.text = textField.text;
Its simple
pass the text of text field in FirstViewController to SecondViewController
using string and assign the passed string to the text field of SecondViewController
For passing the string you can have many option
1) Using Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"secondViewSegue"]) {
SecondViewController *objVC = [segue destinationViewController];
objVC.strText = textfeld.text;
}
2) Passing data directly while pushing
SecondViewController *objVC = [[SecondViewController alloc] initWithNib:#"SecondViewController" bundle:nil];
objVC.strText = textfield.text;
[self pushViewController:objVC animated:YES];
You can pass the variable to the second View Controller in the Segue.
Passing variables between view controllers
I need to pass a PFObject to 2 views, first one is detail and second is a segue from the detail. I can easily pass the PFObject to the detail, using PFObject *obj = [array objectAtIndex:indexPath.row], detailVC.object = object in prepareForSegue to a property of the detail view controller, but when I try to pass from the detail to the third view, using vc.object = self.object in prepareForSegue then the value of the PFObject in third view is nil. What is the problem?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue"]) {
thirdVC *vc = (thirdVC *)segue.destinationViewController;
vc.object = self.object;
}
}
I often found this error when I tried to segue to a VC embedded within its own navigation controller. Make sure it is not, and if it is use this to refer to the controller instead
if ([segue.identifier isEqualToString:#"<#Segue Name#>"]) {
UINavigationController *nav = (UINavigationController *)[segue destinationViewController];
<#UIViewController#> *controller = (<#UIViewController#> *)nav.topViewController;
controller.object = self.object;
}
Other than that, I can only think to log the object inside the second VC's prepareForSegue and see if it exists there
I'm trying to pass data(_claimReportToDetailView) from viewDidLoad (of MasterVC) to DetailVC. It's always null.
#interface LAMasterViewController ()
{
NSArray *_claimReports;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_claimReports = [[LADataModelController getSingleton] getClaimReportsOrderedByAccessedDate];
LADetailViewController *detailViewController = [[LADetailViewController alloc] init];
detailViewController.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
NSLog(#"claim%#",detailViewController.claimReportToDetailView); // captures here properly.
}
#interface LADetailViewController : UIViewController
#property(nonatomic ) LAClaimReport *claimReportToDetailView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"sdfdf%#", _claimReportToDetailView); // logs null always.
}
Your viewDidLoad seems strange. You have this line:
LADetailViewController *detailViewController = [[LADetailViewController alloc]init];
Yet you say that the view controller is on the storyboard.
I think what is happening is that you are creating this VC, and setting it's property, but the Storyboard is loading a completely new VC, for which you haven't set the property.
Usually, the way you pass information to VCs on Storyboards is in the prepareForSegue: method.
You have to pass the data to the details view controller in prepareForSeque method in master view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) { //<-make shire the segue identifier match one in storyboard
_claimReports = [[LADataModelController getSingleton] getClaimReportsOrderedByAccessedDate] ;
LADetailViewController *vc = (LADetailViewController*)[sender destinationViewController];
vc.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
}
}
This should fix your project. Give it a try
//Allocating LADetailViewController instance
LADetailViewController *detailViewController = [[LADetailViewController alloc]init];
//Connecting it the specified VC on storyboard
detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"yourVCID"];
//Now the connection is set, so pass the data you need
detailViewController.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
//Depending on present or push you should try one of the 2 lines
[self.navigationController pushViewController:detailViewController animated:YES];
//or
[self presentViewController:detailViewController animated:YES completion:nil];
This happens because whenever you navigate from one UIViewController to other, it is initialized again, so rather than setting value in viewDidLoad, try to set value on the event when you perform navigation and make navigation through code rather than with segue.
I'm trying to pass an object using unwind segue. My button unwinds successfully, but all the objects in the view are reinitialized or something. I use prepareForSegue method to prepare my object successfully, but when I access the viewController in my destination segue (through an action button) all the objects are either reinitialized or not allocated.
I'm pretty new at this so any suggestions would be appreciated! Here's the code I'm stupefied with.
//prepareForSegue is called in my view controller i'm trying to unwind from.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ReturnInput"])
{
GLUPipeDataController *dataController;
dataController.length = [[NSNumber alloc] initWithFloat:1.5];
self.dataController = dataController;
[self.dataController print];
}
}
//this done is called in my destination view controller. It is linked in IB by control-draging the unbutton to the exit routine on the connections panel. I then updated the name of the unwind segue in the document outline.
- (IBAction)done:(UIStoryboardSegue *)segue
{
if ([[segue identifier] isEqualToString:#"ReturnInput"])
{
GLUEditViewController *editController = [segue destinationViewController];
if (editController.dataController)
{
self.dataController = editController.dataController;
[self.dataController print];
[[self tableView] reloadData];
}
}
if ([[segue identifier] isEqualToString:#"ReturnInserts"]) {
GLUEditInsertsViewController *editInsertController = [segue destinationViewController];
if (editInsertController.listInserts) {
self.dataController.listInserts = editInsertController.listInserts;
[[self tableView] reloadData];
}
}
}
The call [self.dataController print] in prepareForSegue prints property. But the same call in the done method prints a newly initialized object.
I have a ViewController that has a couple of buttons on it created in Interface Builder. The first button will display a popover linked up in IB, it is linked to a UINavigationController and has a TableView under it with a class of PopupViewController.
The second button, I have an action setup for goToCategory and when clicking on that, I want to set a property on the PopupViewController
ViewController.m
//go to category
-(IBAction)goToCategory:(id)sender{
NSLog(#"GO TO CAT");
//PopupViewController *popupVC = [self.storyboard instantiateViewControllerWithIdentifier:#"popoverVC"];
//popupVC.currentLevel = 1;
[self performSegueWithIdentifier:#"popoverSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"seg1");
if ([[segue identifier] isEqualToString:#"popoverSegue"]){
//PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController];
//PopupViewController *popupVC = [segue destinationViewController];
PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0];
popupVC.test = #"just a test";
NSLog(#"seg2");
}
}
PopupViewController.h
#property (nonatomic, strong) NSString *test;
PopupViewController.m
#synthesize test;
-(void)viewDidLoad{
NSLog(#"test: %#", test); //returns test: (null)
}
I've found a lot of answers on SO, hence some of the commented out lines in my prepareForSegue. But none of these set the value of test. PopupViewController *popupVC = [segue destinationViewController]; throws an error due to it referring to the UINavigationController so I can't use that as it is, even though that seems to be the way to do it in a lot of answers I've seen. But no matter which way I try doing it, the output is always null?
UPDATE:
PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController]; and PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0]; from my prepareForSegue above both work on the 6.1 simulator. My iPad's iOS is 5.1.1 which it isn't working on. Is there something different I need to do for iOS 5?
Try this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"seg1");
if ([[segue identifier] isEqualToString:#"popoverSegue"])
{
UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
PopupViewController *popupVC = [navController topViewController];
popupVC.test = #"just a test";
NSLog(#"seg2");
}
}