全局变量未保留在目标C中

时间:2016-04-29 20:39:10

标签: ios objective-c

所以我正在尝试获取一个变量,其中包含我希望在UIweb视图中打开以显示在标签中的PDF文件的名称。我首先在一个单独的类中识别全局变量:

头:

#import <Foundation/Foundation.h>
extern NSString *PDFNameString; 
extern NSString *PDFActualName; 
@interface GlobalVars : NSObject
{

}
@property (nonatomic, strong) NSString *PDFActualName;
@property (nonatomic, strong) NSString *PDFNameString;
@end

的.m:

#import "GlobalVars.h"
NSString *PDFNameString; 
NSString *PDFActualName; //the name of the PDF file

@implementation GlobalVars
{

}

@end

然后我有一个菜单,其中有一个包含文本字段的警告对话框,以便用户可以输入PDF名称:

·H

#import <Foundation/Foundation.h>
#import "GlobalVars.h"

@interface TitleViewController : UIViewController{
    NSFileManager *FM1;
}

@property (retain, nonatomic) IBOutlet UIBarButtonItem *OpenPDF;
@property (retain, nonatomic) IBOutlet UIBarButtonItem *ViewPDF;

-(void)OpeningPDF;
-(void)alertView;

@end

的.m

#import "TitleViewController.h"

@interface TitleViewController ()

@end

@implementation TitleViewController

UIAlertView * alert;
UITextField *textField;
int StopTheAlerts = 0;

- (void)viewDidLoad
{
    [super viewDidLoad];
    FM1 = [NSFileManager defaultManager];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)ButtonClicked:(id)sender {
    self.OpeningPDF; 
}


- (void)dealloc {
    [_OpenPDF release];
    [_ViewPDF release];
    [super dealloc];
}

- (void)OpeningPDF
{
    StopTheAlerts = 0;
    alert = [[UIAlertView alloc]
    initWithTitle:@"Please enter a valid PDF name below:"
    message:@"(Please add .PDF on the end!) \n \n"
    delegate:self cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"ok", nil];
    textField = [[UITextField alloc] init];
    [textField setBackgroundColor:[UIColor whiteColor]];
    textField.delegate = nil;
    textField.borderStyle = UITextBorderStyleLine;
    textField.frame = CGRectMake(15, 90, 255, 30);
    textField.placeholder = @"PDF name";
    textField.keyboardAppearance = UIKeyboardAppearanceAlert; //set up an alert box with a text field
    [textField becomeFirstResponder];
    [alert addSubview:textField];
    [alert show];
    [alert release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    PDFNameString = textField.text; //set the PDF name variable to the name entered
    PDFActualName = textField.text; 
    NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *DocumentDir = [Paths objectAtIndex:0];
    NSString *TempFilePath = PDFNameString; 
    PDFNameString = [DocumentDir stringByAppendingPathComponent:TempFilePath];
    if([FM1 fileExistsAtPath: PDFNameString]){
        _ViewPDF.enabled = true; 
    }
    else {
        while(StopTheAlerts <= 0){
            UIAlertView *ErrorAlert = [[UIAlertView alloc]
            initWithTitle:@"This file dosn't exist!"
            message:@"Please reenter the file name!"
            delegate:self cancelButtonTitle:@"OK"
            otherButtonTitles: nil];
            [ErrorAlert show];
            [ErrorAlert release];
            StopTheAlerts = 1;
        }
    }

}
@end

这一部分一切正常,但是,当我切换包含UIWebView的视图来实际查看PDF时(我已经删除了代码的这一部分)它有旧的“消息发送到解除分配的实例0xdb8fca0“当我尝试将全局变量分配给包含PDF名称的标签时出现错误,当尝试让UIWebView使用其他全局变量读取PDF时,也会发生此错误。

·H

#import <UIKit/UIKit.h>
#import "GlobalVars.h"

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *PDFNameL;

@end

的.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _PDFNameL.text = PDFActualName;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc {
    [_PDFNameL release];
    [super dealloc];
}

@end

如果有人知道问题的解决方案,我会很高兴

感谢

0 个答案:

没有答案