![](/img/trans.png)
[英]Passing values from one ViewController to another in Objective-C
[英]Objective-C Passing NSURL to another viewcontroller
我有兩個視圖控制器: FirstViewController
和SecondViewController
。 該項目是錄制視頻並將其附加到電子郵件中。 我可以記錄下來並可以發送電子郵件,只需要附加它即可。 我可以在FirstViewController
獲取視頻的URL,但是傳遞它不起作用。
FirstViewController-
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.movie"])
{
// Saving the video to phone / // Get the new unique filename
NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath];
UISaveVideoAtPathToSavedPhotosAlbum(sourcePath, self, /*@selector(video:didFinishSavingWithError:contextInfo:)*/nil, nil);
self.movieURL= [[NSURL alloc] initFileURLWithPath: sourcePath];
NSLog(@"movieURL");
NSLog(self.movieURL.absoluteString);
SecondViewController *sendMovie = [[SecondViewController alloc] initWithNibName:@"ViewController" bundle:nil];
sendMovie.movieU = [[NSURL alloc] initFileURLWithPath: sourcePath];
[[self navigationController] pushViewController:sendMovie animated:YES];
NSLog(@"movieU");
NSLog(sendMovie.movieU.absoluteString);
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
要附加到電子郵件的SecondViewController-
NSLog(@"1st print");
NSLog(self.movieU);
[controller addAttachmentData:[NSData dataWithContentsOfURL:self.movieU] mimeType:@"video/MOV" fileName:@"defectVideo.MOV"];
NSLog(@"2nd print");
NSLog(self.movieU.absoluteURL);
[self presentViewController:controller animated:YES completion:nil];
NSURL的SecondViewController.h屬性
@property(nonatomic) NSURL *movieU;
該movieURL
是在一個屬性FirstViewController
,但我不認為這是必要的。
我可以看到的URL FirstVC
,但它似乎為零的SecondVC
。
解決方案 -
1)將NSURL *movieU
定義為extern
NSURL *movieU
而不是屬性。
2)否則將其定義為AppDelegate
的屬性並訪問它。
3)否則synthesize
屬性movieU
並像-
在您的FirstViewController中 -
SecondViewController *sendMovie = [[SecondViewController alloc] initWithNibName:@"ViewController" bundle:nil];
NSURL *url = [[NSURL alloc] initFileURLWithPath: sourcePath];
sendMovie.movieU = url;
[[self navigationController] pushViewController:sendMovie animated:YES];
然后將NSLog
在SecondViewController中 。
希望這可以幫助!
您傳遞信息的方式沒有任何問題。 嘗試這個:
// In you code where you create the second view controller.
SecondViewController *sendMovie = [[SecondViewController alloc] initWithNibName:@"ViewController" bundle:nil];
// SecondViewController viewDidLoad method being called (movieU is still nil)
sendMovie.movieU = [[NSURL alloc] initFileURLWithPath: sourcePath];
// Do a nil test here
if (!sendMovie.movieU) {
NSLog(@"%@",@"It's nil here!"); // If this is the case something is wrong with "sourcePath"
} else {
NSLog(@"%@",@"It's not nil here!");
}
[[self navigationController] pushViewController:sendMovie animated:YES];
// SecondViewController viewWillAppear method being called (movieU is NOT nil)
// SecondViewController viewDidAppear method being called (movieU is NOT nil)
該測試將告訴您很多。 如果此處為nil,則問題不在於設置第二個視圖控制器的屬性,而在於實際創建NSURL
對象。 可能有很多事情,例如您正在第二個視圖控制器中的某個地方覆蓋它(例如, viewWillAppear
方法)。 只需使用斷點,日志記錄等方式跟蹤路徑,即可跟蹤其使用壽命。
編輯-調試步驟
在創建Second VC的地方設置斷點,設置其屬性,推動它,最后查看它(單擊選項卡)。 在第二個VC的整個生命周期中設置斷點( viewDidLoad
, viewWillAppear
, viewDidAppear
等)。 這將使您逐步完成VC的整個生命周期。 如果您尚未使用這些方法,請繼續定義它們,包括對super
的調用。 當您逐步通過這些斷點時,請繼續檢查movieU
的值。 您首先會看到它為nil,然后將其設置為nil(從您的評論中知道),而不是nil,然后變為nil,再次等等。如果您有耐心和系統,則可以找到它並學到很多東西關於VC的生命周期。 如果您不系統地完成操作,則可能會錯過一些東西。 例如,當您單擊選項卡進行實際查看時,您的代碼可能正在創建一個全新的Second VC。 以這種方式單步執行代碼將顯示這一點。
編輯
確保您正在viewWillAppear
/ viewDidAppear
方法或更高版本中進行測試,以便實際上已設置該屬性。 查看我的代碼中的注釋。
編輯
在沒有所有權來源的情況下合成的可保留對象指針類型的屬性具有其關聯實例變量的所有權(如果已存在); 否則,[從Apple 3.1開始,LLVM 3.1開始],其所有權隱含地強大。 在此修訂版之前,合成此類屬性的格式不正確。
來自: http : //clang.llvm.org/docs/AutomaticReferenceCounting.html#property-declarations
據此(除非您在ivar上指定弱-不太可能),默認情況下它應為強。
祝好運。
用這個
self.movieURL = [NSURL fileURLWithPath:sourcePath];
代替
self.movieURL= [[NSURL alloc] initFileURLWithPath: sourcePath];
希望這項工作
我把它修好了! 我決定使用單例,該單例通過另一個類使用共享實例。 我創建了一個名為Singleton的全新類,只包含了sharedinstance方法:
+ (id)sharedInstance {
static id sharedInstance = nil;
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
單例
@property NSURL *movieU;
+ (id)sharedInstance;
確保將單例類導入到First VC和Second VC中。
SecondVC.m
Singleton* single = [Singleton sharedInstance];
[controller addAttachmentData:[NSData dataWithContentsOfURL: single.movieU] mimeType:@"video/MOV" fileName:@"defectVideo.MOV"];
FirstVC.m
Singleton* single = [Singleton sharedInstance];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.movie"]){
// Saving the video / // Get the new unique filename
NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath];
UISaveVideoAtPathToSavedPhotosAlbum(sourcePath, self, /*@selector(video:didFinishSavingWithError:contextInfo:)*/nil, nil);
self.movieURL = [[NSURL alloc] initFileURLWithPath: sourcePath];
NSLog(@"movieURL");
single.movieU = self.movieURL;
通過這樣做,它成功地將包含要記錄的視頻信息的URL傳遞給電子郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.