gpt4 book ai didi

ios - 将 Tableview 单元格的文本传递给 RootViewController

转载 作者:行者123 更新时间:2023-11-29 03:05:57 25 4
gpt4 key购买 nike

我是 iOS 编程的新手。我已经阅读了很多关于这个问题的线程,但也无法弄清楚到底是什么导致了错误。我需要将 bookmarkViewController tableview 的第一个 tableview 单元格文本传回 myViewController 的 URL 地址栏,当我单击第一个 tableview 的单元格时,它是根 ViewController。除了将此数据传递给根 ViewController 之外,一切正常,我正在为此使用委托(delegate)。

我的ViewController.h

#import "bookmarkViewController.h"
#import <UIKit/UIKit.h>
#import "historyViewController.h"
#import "AppDelegate.h"
#import "Reachability.h"

@interface myViewController : UIViewController <UITextFieldDelegate,UIAlertViewDelegate,secondViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *addButton;

@property (weak, nonatomic) IBOutlet UITextField *addressBar;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UITextField *searchBar;
- (IBAction)goBack:(id)sender;
- (IBAction)goForward:(id)sender;
- (IBAction)refreshWebView:(id)sender;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goBackButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *goForwardButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshButton;
-(void)textFieldDidEndEditing:(UITextField *)textField;




- (void) loadWebPageFromString:(NSString *)string;


@end

myViewController.m

   #import "myViewController.h"


@interface myViewController ()

@end

@implementation myViewController



-(void)loadView
{


[super loadView];
[_goBackButton setEnabled:NO];

}

- (void)webViewDidFinishLoad:(UIWebView *)webview
{


_addButton.enabled=YES;
_refreshButton.enabled=YES;
if ([_webView canGoBack]) {
[_goBackButton setEnabled:YES];

} else [_goBackButton setEnabled:NO];

if ([_webView canGoForward]) {
[_goForwardButton setEnabled:YES];

} else _goForwardButton.enabled=NO;


}

- (void)checkForWIFIConnection {
Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];

NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

if (netStatus!=ReachableViaWiFi)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
message:@"Your device is not connected to internet."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];



}
}







- (void)viewDidLoad
{

_refreshButton.enabled=NO;
_addButton.enabled=NO;
[_goBackButton setEnabled:NO];
_goForwardButton.enabled=NO;
[self checkForWIFIConnection];

[super viewDidLoad];
}


-(void)textFieldDidEndEditing :(UITextField *)textField
{


if (textField.text){

if (textField==_searchBar){

AppDelegate *historydelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *a=historydelegate.historyArray;
NSString *b=[NSString stringWithFormat:@"http://www.google.com/search?q=%@",textField.text] ;
[a addObject:b];

}}}



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


- (IBAction)googleEntered:(UITextField *)sender {


Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];

NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

if (netStatus==ReachableViaWiFi){
[self loadWebPageFromString:_searchBar.text];
[self textFieldDidEndEditing:_searchBar];
_addressBar.text=[NSString stringWithFormat: @"http://www.google.com/search?q=%@",_searchBar.text] ;
}else {[self checkForWIFIConnection];self.addressBar.text=NULL;}


}


- (IBAction)goBack:(id)sender {
[_webView goBack];

_searchBar.text=NULL;
_addressBar.text=NULL;
}

- (IBAction)goForward:(id)sender {
[_webView goForward];
_searchBar.text=NULL;

}

- (IBAction)refreshWebView:(id)sender {
[_webView reload];


[self checkForWIFIConnection];


}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

AppDelegate *maindelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *a=maindelegate.bookmarksArray;
if([segue.identifier isEqualToString:@"segueOne"]){

if ([_addressBar.text length]!=0){
NSString *b=[NSString stringWithFormat:@"%@",_addressBar.text];
[a addObject:b];



}}}

- (void) loadWebPageFromString:(NSString *)string {
NSURL *url = [NSURL URLWithString:string];





NSString *googleSearch = [string stringByReplacingOccurrencesOfString:@" " withString:@"+"];
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", googleSearch]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[_webView loadRequest:request];
}


-(void)loadAddress:(NSString *)mystring{

NSURL *myurl = [NSURL URLWithString:mystring];
if(!myurl.scheme){
myurl = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", mystring]];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:myurl];
[_webView loadRequest:request];}
else{
myurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@", mystring]];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:myurl];
[_webView loadRequest:request];

}

}


-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{



if (navigationType==UIWebViewNavigationTypeLinkClicked) {

NSURL *URL=[request URL];

[self checkForWIFIConnection];
if ([URL scheme] ) {
Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];

NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

if (netStatus==ReachableViaWiFi ){
_addressBar.text=URL.absoluteString;
_searchBar.text=NULL;
[_webView loadRequest:request];
AppDelegate *historydelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *a=historydelegate.historyArray;
[a addObject:URL.absoluteString];
}




}
return NO;
}
return YES;

}

- (BOOL) urlIsValid: (NSString *) url
{
NSString *regex =
@"((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?";
/// OR use this
///NSString *regex = "(http|ftp|https)://[\w-_]+(.[\w-_]+)+([\w-.,@?^=%&:/~+#]* [\w-\@?^=%&/~+#])?";
NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

if ([regextest evaluateWithObject: url] == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid URL"
message:@"Enter valid URL."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}

return [regextest evaluateWithObject:url];
}

-(IBAction)addressEntered:(UITextField *)sender {
Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];

NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

if (netStatus==ReachableViaWiFi && [self urlIsValid:_addressBar.text]){



[self loadAddress:_addressBar.text];
AppDelegate *historydelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *a=historydelegate.historyArray;

[a addObject:_addressBar.text];

}else [self checkForWIFIConnection];
[sender resignFirstResponder];
_searchBar.text =NULL;

}
-(void)showView{
bookmarkViewController *b=[[bookmarkViewController alloc]initWithNibName:@"bookmarkViewController" bundle:nil];

b.delegate=self;

[self presentViewController:b animated:YES completion:nil];

}
- (void)passData:(NSString *)data
{


self.addressBar.text=data;

}
- (IBAction)go:(id)sender {
[self showView];
}

@end

书签 View Controller .h

@protocol secondViewControllerDelegate <NSObject>

@required

- (void)passData:(NSString *)data;

@end


#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "myViewController.h"
@interface bookmarkViewController : UIViewController
@property (nonatomic, weak) id<secondViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UITableView *tableView;


- (IBAction)goBac:(id)sender;



@end

bookmarkViewController.m

#import "bookmarkViewController.h"
#import "AppDelegate.h"
@interface bookmarkViewController ()

@end

@implementation bookmarkViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.

return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
AppDelegate *maindelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *a=[maindelegate bookmarksArray];
cell.textLabel.text = [a objectAtIndex:[indexPath row]];
return cell;


}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if ([indexPath row]==0) {

if ([self.delegate respondsToSelector:@selector(passData:)]) {


[self.delegate passData:cell.textLabel.text];


}
[self dismissViewControllerAnimated:YES completion:nil];
}
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
AppDelegate *maindelegate= (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *a=[maindelegate bookmarksArray];
return [a count];
}


- (void)viewDidLoad
{



[super viewDidLoad];
[[self tableView] reloadData];

}

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



- (IBAction)goBac:(id)sender {

[self dismissViewControllerAnimated:YES completion:nil];
}
@end

ScreenShot

最佳答案

当选择一行时,不应使用 dequeueReusableCellWithIdentifier: 方法。仅当您实际上想要获得可重复使用的单元时才应使用它。您可以使用:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];

if ([indexPath row]==0)
{
if ([self.delegate respondsToSelector:@selector(passData:)])
{
[self.delegate passData:cell.textLabel.text];
}
}
}

您不应该从 subview Controller 调用 dismissViewControllerAnimated:completion: 方法,这是父 View Controller 的工作。在你的委托(delegate)方法中:

- (void) passData:(NSString *)data
{
self.addressBar.text = data;
[self dismissViewControllerAnimated:YES completion:nil];
}

编辑:方法名称已更新。

关于ios - 将 Tableview 单元格的文本传递给 RootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22771525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com