ios - 如何在objective-c中的 View Controller 之间传递数据?
<p><p>选择项是一个 slider 。当我单击 slider 时,它会将数据传递给第二个 VC(WebViewController)。但是如何在objective-c中将数据从第一个 ViewController 传递到第二个 ViewController ?抱歉,这是我第一次编写 Objective-C 。</p>
<p>第一个 VC .m 文件</p>
<pre><code>#import "WebViewController.h"
- (void)viewDidLoad {
;
arraySliderProducts = [init];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
;
UIViewController *controller = nil;
switch (indexPath.row)
{
case 0:
{
WebViewController *WebViewController = [ init];
//error: No visible @interface for "WebViewController" declares the selector 'alloc'
WebViewController.data = arraySliderProducts[@"title"]; //pass this link value
//error: Property 'data' not found on object of type 'WebViewController'
;
}..
</code></pre>
<p>第二个 VC .m 文件</p>
<pre><code>@interface WebViewController ()
@property (nonatomic, retain) NSString *data;
</code></pre>
<p>第二个 VC .h 文件</p>
<pre><code>#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController
{
AppDelegate *appDelegate;
NSString *data;
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>注意两点:</p>
<p>1:这不是启动 ViewController 以在您的应用中显示/呈现的方式。</p>
<p>2:您应该在您的 SecondVC 的 <code>.h</code> 文件中声明您的 <code>NSString *data</code>。 </p>
<p>现在第 1 点解决方案是在您的 <code>didSelectItemAtIndexPath</code> 中更改您的代码:函数</p>
<pre><code>switch (indexPath.row)
{
case 0:
{
// By Default storyboard name is "Main". Change it if you you have different name in your project
UIStoryboard *mainStoryboard = ;
// Now instantiate your view controller with its identifier on this storyboard object.
// Note: don't forget to set viewControllers' identifier
WebViewController *vc = ;
// Now Pass your value
WebViewController.data = arraySliderProducts[@"title"];
//Finally push this object on your navigation stack
;
}...
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在objective-c中的 ViewController 之间传递数据?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46108639/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46108639/
</a>
</p>
页:
[1]