return to collectionView from detail view - uiviewcontroller

I have an app that I load a GameScene form a CollectionView. I load the view just fine, but when I'm done I can't seem to be able to return to the collectionView.
I have tried
[self dismissModalViewControllerAnimated:YES];
and
[GameScene dismissModalViewControllerAnimated:YES];
and
[collectionViewController dismissModalViewControllerAnimated:YES];
but they all say No Visible #Interface For viewController declares the selector dismissModalViewControllerAnimated:
Here is the code where I load the GameScene from the collectionView
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row==0) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"GameSceneOne"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
NSLog(#"mycell.image1.image:%#",myCell.image1.image);
[self presentViewController:vc animated:YES completion:NULL];
}}
Any Ideals of how to dismiss the gameScene and return to the CollectionView?

Related

How to parse html tag url and make it clickable in a label

I need to parse a html tag. For exemple:
<a href=“http://www.google.it”>MY LINK</a>
So, how can I detect this link, and make it clickable in a label? Only the link clickable.
Thanks
You can just use TextView. With textView you will set that the attributedText has an attribute that is the link you want
NSString *url = #"https://www.stackoverflow.com";
NSString *stringLink = #"My link";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:stringLink attributes:nil];
[attrString addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, [attrString.string length])];
self.textView.attributedText = attrString;
Edit:
Make sure to format correctly your String to be sure to catch your url after "href=\"" and the stringLink after ">" character.
Try this way In ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextView *textView;
#end
And ViewController.m
#import "ViewController.h"
#interface ViewController ()<UITextViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapOnTextView)];
[self.textView addGestureRecognizer:tapGesture];
[tapGesture setNumberOfTapsRequired:1];
[self.view addSubview:self.textView];
self.textView.delegate=self;
NSString * testStr=#"The world’s most popular camera is more advanced than ever. The 12-megapixel iSight camera captures sharp, detailed photos. It takes brilliant 4K video, up to four times the resolution of 1080p HD video. https://www.google.com/gmail/about/# iPhone 6s also takes selfies worthy of a self-portrait with the new 5-megapixel FaceTime HD camera. And it introduces Live Photos, a new way to relive your favourite memories. It captures the moments just before and after your picture and sets it in motion with just the press of a finger.";
self.textView.text = testStr;
self.textView.dataDetectorTypes = UIDataDetectorTypeLink;
}
- (void)openScheme:(NSString *)scheme {
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
if ([application respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:#{}
completionHandler:^(BOOL success) {
NSLog(#"Open %#: %d",scheme,success);
}];
} else {
BOOL success = [application openURL:URL];
NSLog(#"Open %#: %d",scheme,success);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)tapOnTextView
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.textView.text];
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
[detector enumerateMatchesInString:self.textView.text options:kNilOptions range:NSMakeRange(0, [self.textView.text length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(#"Values: %#", result);
if (result.resultType == NSTextCheckingTypeLink)
{
NSLog(#"matched: %#",result.URL);
[self openScheme:[result.URL absoluteString]];
}
}];
self.textView.attributedText=attributedString;
}

iOS JSON Parsing to tableview, incorrect rows

I am developing an iOS App, I have a tableview controller populated from a MySQL database. When I click on a raw a detailview appears showing data from the database, such as a master detail app. The problem is following:
1. When I run the app on the iPhone simulator, the app shows the three records from the mysql table, thats correct.
2. If I click on the first row, nothing happened.
3. If I click on the second or third row, the app opens the detailview, but the data don't correspond to the selected row(record).
4. I click on the back button of the detailview, and the app opens the masterview again.
5. Now if I click on the first row, the app opens the detailview, but as mentioned before, the detailview data don't correspond to the selected row(record) on the master view.
Here you have my viewcontroller code (master view):
//
// ViewController.m
//
#import "ViewController.h"
#import "DetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Areas";
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
NSURL *url = [NSURL URLWithString:#""];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
[mainTableView reloadData];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"The download could not complete - please make sure you're connect to either 3G or WiFi" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [news count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"nombre"];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:#"nombre"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here the detailview code:
//
// DetailViewController.m
//
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize newsArticle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
titleLabel.text = [newsArticle objectForKey:#"nombre"];
timeLabel.text = [newsArticle objectForKey:#"name"];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Any idea what I have done wrong??
Thank you
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.title =[[news objectAtIndex:indexPath.row]objectForKey:#"nombre"];
detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
You are using didDeselectRow which is called when a row is deselected and hence the wierd behaviour plus wrong data.. you have to use didSelectRow

UITableViewController refresh control not showing

I've just implemented a UITableView controller and for some reason the refresh control isn't showing up at the top is there something that I've got wrong?
ScoresNotificationsPage
- (id)initWithFrame:(CGRect)frame {
self = [self initWithStyle:UITableViewStylePlain];
self.view.backgroundColor = [UIColor whiteColor];
self.view.frame = frame;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor redColor];
// Assign the refresh control to the table view controllers refresh property.
[refreshControl addTarget:self action:#selector(changeSorting) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
MainViewController
Adding the ScoresNotificationsPage with
[self.view addSubview:scoresNotificationsPage.view];

Unable to "pass data" from a UITableViewController to a UIViewController

I am in troubles :)
I am unable to acheive my project because i cannot pass the data from my UITableView to a DetailView (UIViewController)
I am a beginner so i certainly do something wrong but i don't know what. I have red several tutorials and it seems to be ok ... but it's not !
Here is the .h of my UITableViewController :
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "XMLParser.h"
#import "ColzaDetailViewController.h"
#interface ColzaViewController : UITableViewController <XMLParserDelegate>
{
XMLParser *parser;
NSDictionary *colzaInfos;
}
#property (nonatomic, retain) NSDictionary *colzaInfos;
#end
I have create an NSDictionary to store the data I need to pass to the detailView (ColzaDetailViewController)
Here is the part of my UITalbeViewController .h wich is interresting for my problem :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ColzaDetailViewController *detailViewController = [[ColzaDetailViewController alloc] initWithNibName:#"ColzaDetail" bundle:[NSBundle mainBundle]];
colzaInfos = [parser.stories objectAtIndex:indexPath.row];
detailViewController._colzaInfos = colzaInfos;
[self.navigationController pushViewController:detailViewController animated:YES];
NSLog(#"TEST MainView : %#", detailViewController._colzaInfos);
detailViewController = nil;
}
I think everything is ok here. I have put a NSLog (TEST MainView) to check if there is something in my NSDictionary _colzaInfos.
So here are my .h and .m of my DetailVieuw (UIVIewController)
.h
#import <UIKit/UIKit.h>
#import "ColzaViewController.h"
#interface ColzaDetailViewController : UIViewController
{
IBOutlet UILabel *colzaSettle;
NSDictionary *_colzaInfos;
}
#property (nonatomic, strong) NSDictionary *_colzaInfos;
#property (nonatomic, retain) IBOutlet UILabel *colzaSettle;
#end
.m
#import "ColzaDetailViewController.h"
#implementation ColzaDetailViewController
#synthesize _colzaInfos, colzaSettle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
colzaSettle = [_colzaInfos objectForKey:kCloture];
NSLog(#"TEST DetailView : %#", _colzaInfos);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
As you can see in the viewDidLoad, i have put a second NSLog (TEST DetailView) in order to check one more time if there is something in my Dictionary
And here are the log :
2012-03-14 16:23:54.240 Mobile Settles[7173:f803] TEST DetailView : (null)
2012-03-14 16:23:54.241 Mobile Settles[7173:f803] TEST MainView : {
date = "13/03/2012\n ";
echeance = "Ao\U00fbt 2012\n ";
settle = "453.25\n ";
variation = "5.75";
}
So As you can see the log for DetailView is NULL but in the MainView contains data.
But i need to get those data in the DetailView in order to display them.
The only thing it seem "strange" for me at this step is the _colzaInfos Dictionary is not alloc and init at anytime ... But i have try to allocate it and initialize it in the .m of the detailViewController but my log was at this time
TEST DetailView : {}
Someone can help me to understand what i am doing wrong.
(if you need another part of my code to check something ... feel free to ask.)
Thanks a lot for help
As you told me to do, i have put the line of code at this place, please let me know if i am wrong. And as i told in the comment, When i put a breakpoint directly in this part of code and when i Run the program, the breakpoint stop the process AFTER the NSLog. And if i try to alloc/init it before, i have nothing : TEST detailView : { }.
Sorry but it does not work :(
The code :
#implementation ColzaDetailViewController
#synthesize _colzaInfos, colzaSettle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
if (! _colzaInfos)
_colzaInfos = [[NSDictionary alloc] init];
}
return self;
}
Add this to your initWithNibName method in ColzaDetailViewController.m.
if (! _colzaInfos) _colzaInfos = [[NSDictionary alloc] init];
It is same issue if you're using an NSArray. You have to initialize the variable at some point for it to be able to hold data. You've only declared it in the .h file.

When selecting a row in table, it switches to blank screen

in part of my app (storyboard) I have a table view with various rows, and when I select a row, an html file should be displayed. Instead when I select a row, I get a blank screen instead.
I thought I needed another view controller after the tableview controller, but I get the same issue whether I have one or not.
In my table controller .m file I have the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
articleIndex = indexPath.row;
WebView *selectedArticle = [[WebView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:selectedArticle animated:YES];
[selectedArticle release];
That is allowing me to select a row and the screen changes to a blank one, rather than loading the relevant html file.
I have in my resources folder a .h and .m file as follows:
webView.h
#import <UIKit/UIKit.h>
#import "globals.h"
#interface WebView : UIViewController{
NSArray *articleNames;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#end
webView.m
#import "WebView.h"
#implementation WebView
#synthesize webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
articleNames = [[NSArray arrayWithObjects:
#"chapter1",
#"chapter2",
#"chapter3",
#"chapter4",
#"chapter5",
#"chapter6",
nil] retain];
NSString *chapterName = [[NSString alloc] initWithString:[articleNames objectAtIndex:articleIndex]];
NSString *path = [[NSBundle mainBundle] pathForResource:chapterName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[chapterName release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
I can't see what I have done wrong here, any advice? I'm not sure how to get the html files to display or where I need to make changes to allow it. If I remove the HTML files from the project the app crashes so it's definitely loading them but for some reason not displaying...
Well first off where is "articleIndex" I see you set it in your tableViewController but it is not webView.h or webView.m. I would suggest first adding in a NSLog
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"Article URL:%#", url);
This will let you see if the url is correct first. Second it looks like you connected your UIWebView with interface builder, so make sure that the webView is connect correctly. You can also test the webView buy using:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: #"http://www.apple.com"]];
And see if apple.com loads. If it does then you know your issue is with your html files or the paths to them.