我有一个小应用程序,默认情况下youtube作为主页。你可以在一个小菜单中更改它,但它不起作用,我不知道为什么。
这是代码
viewController
.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UIWebView *webView;
}
@property (retain, nonatomic) NSArray *array;
@property (retain, nonatomic) NSString *string1;
-(NSString *) dataFilePath;
- (IBAction)settings:(id)sender;
- (IBAction)home:(id)sender;
@end
.m
#import "ViewController.h"
#import "settings.h"
@implementation ViewController
@synthesize array;
@synthesize string1;
- (void)viewDidLoad
{
[super viewDidLoad];
if ([string1 isEqual:@"youtube"]) {
NSMutableArray *Array = [[NSMutableArray alloc] init];
[Array addObject:@"http://www.youtube.com"];
[Array writeToFile:[self dataFilePath] atomically:YES];
}
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
array = [[NSArray alloc] initWithContentsOfFile:filePath];
}
NSString *string = [NSString stringWithString:[array objectAtIndex:0]];
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:string]]];
}
-(NSString *) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"url.plist"];
string1 = @"youtube";
}
- (IBAction)settings:(id)sender {
settings *NView = [[settings alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:NView animated:YES];
}
- (IBAction)home:(id)sender {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
array = [[NSArray alloc] initWithContentsOfFile:filePath];
}
NSString *string = [NSString stringWithString:[array objectAtIndex:0]];
NSLog(@"%@\n",string);
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:string]]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
Settings //here's the menu where I can change the homepage
.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface settings : UIViewController{
ViewController *viewCont;
IBOutlet UITextField *field;
}
-(IBAction)back:(id)sender;
- (IBAction)setHP:(id)sender;
@end
.m
#import "settings.h"
#import "ViewController.h"
@implementation settings
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib
}
- (void)viewDidUnload
{
field = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(IBAction)back:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)setHP:(id)sender {
NSMutableArray *Array = [[NSMutableArray alloc] init];
[Array addObject:field.text];
[Array writeToFile:[viewCont dataFilePath] atomically:YES];
[viewCont.string1 initWithString:@"other" ];
NSLog(@"%@\n", viewCont.string1);// HERE XCODE SAYS string1 = null!!! WHY?
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
所以谁知道我做错了什么?请帮助我,我在网上搜索了4天,但没有找到任何东西!
答案 0 :(得分:1)
我不太了解你的问题,但我注意到你没有在viewCont
行动中初始化setHP:
。
尝试添加此行:
ViewController *viewCont=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
之前
[viewCont.string1 initWithString:@"other" ];