Passing value of URL link by NSString - ios

The problem is here: when i try to pass link string from one controller to another, somehow it lose part of text, and finally my NSString, which i named self.entityLinkToWiki = Московский_Кремль, not http://ru.wikipedia.org/wiki/Московский_Кремль as it should be!
My app have structure of table view, firstly, it load tableview, after that it pass values from XML which depends of indexPath.row. All values except web links works just fine, and i will explain the way i did this. There is part of web link in xml file:
http://ru.wikipedia.org/wiki/Московский_Кремль
To get that, i did following:
In "first" (tableView list) controller i pass values like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSString *string = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"description"];
DetailViewController *detailController = segue.destinationViewController;
detailController.descriptionStringShort =string;
....a lot of code here
detailController.fourthPhotoString = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"imageFourth"];
detailController.webLink = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]
objectForKey:#"webpage"];
}
}
Image link has nothing to do with web link, i just post it, so you get picture that i pass values in this way and it work fine. Then, i pass values from "detail" controller to controller, that finally have UIWebView:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
.... a lot of code here
if ([segue.identifier isEqualToString:#"WebSegue"]){
WebViewController *webController = segue.destinationViewController;
webController.entityLinkToWiki = self.webLink;
NSLog(#"WebSegue link is %#", self.webLink);
}
}
And NSLog says, that self.webLink is already - Московский_Кремль (incorrect value). Therefore, i didn't get correct link for my UIWebView. So now, my question is - how to get correct link passing it like i did? I don't want to bother load it from XML file in final controller, because i need information of indexPath.row, to get correct value (i have a lot of entities in XML file).
Thank you for any help you provide

Related

pass data from UITableCell to UITabView

I am currently working on a project that requires a list of customers to be displayed in a UITableView, the associated cell then segues to a TabView to display a detailed customer record in a tabbed ui.
I have setup the story board with the required TableView and populated fine. The TabViews all setup and I have added a custom class to the main TabView controller which can take the ID (required to interrogate service and return further data) and Customer Name. I have also added a UIViewController for the first tab in which I need to get the ID value.
I can't seem to get hold of the ID or Company Name that is passed. I have tried importing the .h file of the UITabView. I know the UITabView .h file is being populated with the values as in the .m file I am using the Customer Name to update the title of the Navigation Bar. However, whenever I breakpoint on line that gets the ID in the .m file for the individual tab, it always returns nil.
I am using the following code to try and get this value:
companyTabController *headerData = [companyTabController alloc];
_companyName_lbl.text = headerData.companyName;
_companyID_lbl.text = headerData.ID;
I have tried several variations of the above and all to no avail.
You can also use NSUserDefaults to save the data, I think that is the simplest way to save the data throughout the app.
From the code you posted, the headerData is a new instance. So the companyName and the ID will be nil unless you assign some value to them.
Since, you mentioned that you are able update the navigation bar title, try using the same object for fetching the values in this controller as well. (Maybe you can use a singleton object)
If your segueing you have to use the prepareForSegue:sender: method as such:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
companyTabController *companyTC = [segue destinationViewController];
companyTC.companyName_lbl.text = headerData.companyName;
etc
}
if your not segueing you will have to instantiate it as such :
- (void) didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
companyTabController *companyTC = [self.storyboard instantiateViewControllerWithIdentifier:#"CopmanyTabController"];
companyTC.companyName_lbl.text = selectedCell.textLabel.text or = headerData.companyName;
[self.navigationController pushViewController:companyTC animated:YES];
}

In iOS how to get object fromViewController name?

I know that this is double question. I know that I can use property in toViewController to get name of UIViewController to get NSString which tells me where I am coming from.
Anyway I want to ask if there a simple way to get name of UIViewController when unwinding from segue.
I have a UIViewController with segues to 3 forms. I programatically return to that view controller. I need to run a specific code only when I am returning from one of view controllers. My goal is using string from name of fromViewController start that specific code.
Using UIViewController by NSString from its class name isn't safe enough because the name can be changed.
You can use isKindOfClass instead:
UIViewController *destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[MyViewControllerClass1 class]]) {
// put code related to transition to MyViewControllerClass1
}
else if ([destinationViewController isKindOfClass:[MyViewControllerClass2 class]]) {
// put code related to transition to MyViewControllerClass2
}
You can use:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIViewController *destinationViewController = segue.destinationViewController;
NSString * identifier = destinationViewController.identifier;
NSString * title = destinationViewController.title;
}
Create a Custom delegate method in the primary VC, create 3 strings with unique name so that u can identify.
EG.
NSString* stringFrmFORM1, *stringFrmFORM2, *stringFrmFORM3;
-(void)setString:(NSString*)myString{
//set the string from the VC1,2,3 to each string based on Primary VC's strings
}
Call the delegate method from each registration VC, and set those Strings.
You will have your registration strings to each of the Unique strings that you have set, from each of the Registration VC's.
To answer your base question, you can get the name of a class in string form with:
NSString *strClassName = NSStringFromClass([fromViewController class]);
but as #AlexPeda pointed out in ze answer, -isKindOfClass: would be better.
if ([fromViewController isKindOfClass:[SpecificViewController class]]) {
//run your 'specific' code
}

Web page didn't open in iOS app

I have an iOS app which has XML file, that holds data for an app. That file contains links for web pages. For some reason, when I try to display this links with UIWebView, its not showing. There is white screen with black rectangle -
I know this problem is in my web links, but its perfectly correct when I try to open it in Mac or Windows browsers (in fact, I just copy-paste it from Wiki). For example, there is one of my links: http://ru.wikipedia.org/wiki/Московский_Кремль
And there is my code. It simple, but it do work when i test it with other link, like: http://apple.com
Code is below:
-(void)loadWebView{
NSURL *urlik = [NSURL URLWithString:self.entityLinkToWiki];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlik];
[self.myWebView loadRequest:requestObj];
}
// Note: self.entityLinkToWiki look like shown above - http://ru.wikipedia.org/wiki/Московский_Кремль
Of course I just don't want to force user to quit app and open link in Safari. Is there any way to show my stored web links in internal browser? And why is there such strange error? I mean links, that pretty fine in PC/Mac browsers doesn't work here?
UPDATED.
Guys, that was my fault and i terribly sorry for confusing you, and thank you for help. The problem is here: when i try to pass link string from one controller to another, somehow it lose part of text, and finally self.entityLinkToWiki = Московский_Кремль, not http://ru.wikipedia.org/wiki/Московский_Кремль as it should be!
My app have structure of table view, firstly, it load tableview, after that it pass values from XML which depends of indexPath.row. All values except web links works just fine, and i will explain the way i did this. As you request, there is part of web link in xml file:
<webpage>http://ru.wikipedia.org/wiki/Московский_Кремль</webpage>
To get that, i did following:
In "first" (tableView list) controller i pass values like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSString *string = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:#"description"];
DetailViewController *detailController = segue.destinationViewController;
detailController.descriptionStringShort =string;
....a lot of code here
detailController.fourthPhotoString = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:#"imageFourth"];
detailController.webLink = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]
objectForKey:#"webpage"];
}
}
Image link has nothing to do with web link, i just post it, so you get picture that i pass values in this way and it work fine. Then, i pass values from "detail" controller to controller, that finally have UIWebView:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
.... a lot of code here
if ([segue.identifier isEqualToString:#"WebSegue"]){
WebViewController *webController = segue.destinationViewController;
webController.entityLinkToWiki = self.webLink;
NSLog(#"WebSegue link is %#", self.webLink);
}
}
And NSLog says, that self.webLink is already - Московский_Кремль (incorrect value). Therefore, i didn't get correct link for my UIWebView. So now, my question is - how to get correct link passing it like i did? I don't want to bother load it from XML file in final controller, because i need information of indexPath.row, to get correct value (i have a lot of entities in XML file).
Thank you for any help you provide, and sorry about first post (in which i didn't bother to look at console and see, that my link was incorrect).
You have asked multiple questions. If you just want to open the Kremlin link in your browser - Use NSUTF8StringEncoding.
NSString doesn't have any function built in that really does URL encoding properly, but this one works for extended characters. This will definitely handle your foreign characters. I tested the following -
- (void) viewDidLoad{
[super viewDidLoad];
self.myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.myWebView.delegate = self;
NSString *myString = #"http://ru.wikipedia.org/wiki/Московский_Кремль";
NSURL *urlik = [NSURL URLWithString:[myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlik];
[self.myWebView loadRequest:requestObj];
[self.view addSubview:self.myWebView];
}

can i share Facebook user.name and user.id with multiple views

i have worked on this project for a couple of months and i have only been programming for about a year so im not to smart on this subject so please be patient, i have create a login with Facebook and once i fetch the user info i have created a method to parse the info to the login view
and i place this method in the
and this method pushes to the loginViewController once it has received the info, im try to pass the same info to the settingViewController to show the name and the pic which is user.name and user.id and i did the same steps and use the same protocols and it doesnt work now my question is, because im useing the info in the loginViewController i cant place or use them in the settingViewController because im using the info already. could you please help me out with this i have only just in the last month sort of rapped my head around using protocols.
Make sure you have set an identifier for the segue:
and set profilePicture (your case) as a public property in the 2nd view controller.
Then in your 1st view controller, simply use prepareForSegue: to pass your profilePicture property:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"SecondVCSegue"]) {
SecondViewController *secondVC = segue.destinationViewController;
secondVC.profilePicture = profilePicture;
}
}
UPDATE:
have you identified the view controller in your storyboard ?
Store all your fetched data in an custom class and pass the object to your destination view controller using
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
using something like this here UBO is my Custom class and user is facebook user object... that object contains id, image and email address..
UBO = [[UserBusinessObject alloc]init];
UBO.userid=user.id;
NSLog(#"%#",UBO.userid);
UBO.email=[user objectForKey:#"email"];
NSLog(#"%#",UBO.email);
NSString *imageUrl=[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?type=normal",UBO.userid];
NSLog(#"%#image returning",imageUrl);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
//NSLog(#"image data %#",imageData);
UBO.profilePic=imageData;

Delete existing data from another view

I trying to build app like native Apple Notes. And I have a question\problem.
In apple notes when you open already existing note again and delete all text from it - note deleting and data from core data deleting too. How can I do it? First view of app - List of notes, and second view - note. I can't understand how delete that object, what I need. For example: when my segue from list to note look like that:
-(void) prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender{
NoteDetailVC *destViewController = segue.destinationViewController;
if([[segue identifier] isEqualToString:#"ShowNote"]){
NSManagedObject *selectedNote = [self.notes objectAtIndex[[self.tableView indexPathForSelectedRow] row]];
destViewController.selectedNoteInfo = selectedNote;
}
}
And in NoteDetailVC I interact with data some like that:
if (selectedNoteInfo){
// bla bla bla code
}
On create I use setValue: command and else. I understand how dismiss controller without saving data before I set new value. But don't understand how delete already existing object from core data. How check what index I need and etc? Help please! :-)
And sorry for my English again :)
Here is the approach I would take, given I am understanding your question correctly.
When the user chooses to create a new note, create your NSManagedObject to represent that.
Note *newNote = [NSEntityDescription insertNewObjectForEntityForName:#"Note" inManagedObjectContext:self.context];
When they go back to the list or press Done... In prepareForSegue:sender: check the contents of the note.
if (note.contents.length == 0) {
[self.context deleteObject:note];
}
[self.context save:&error]

Resources