Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何跟踪tableview单元格';s文本来自viewcontroller的字段文本?_Ios_Objective C_Swift_Protocols_Delegation - Fatal编程技术网

Ios 如何跟踪tableview单元格';s文本来自viewcontroller的字段文本?

Ios 如何跟踪tableview单元格';s文本来自viewcontroller的字段文本?,ios,objective-c,swift,protocols,delegation,Ios,Objective C,Swift,Protocols,Delegation,我想观察相关视图控制器中tableview单元格文本字段文本的变化 我在单元格中有一个文本字段。它符合名为“RouteChangesInfoTableViewCellDelegate”的协议 @protocol RouteChangesInfoTableViewCellDelegate <NSObject> @required - (void)textFieldDidChange:(NSString *)textNote; @end 相关的viewcontroller也符合协议 @

我想观察相关视图控制器中tableview单元格文本字段文本的变化

我在单元格中有一个文本字段。它符合名为“RouteChangesInfoTableViewCellDelegate”的协议

@protocol RouteChangesInfoTableViewCellDelegate <NSObject>
@required
- (void)textFieldDidChange:(NSString *)textNote;
@end
相关的viewcontroller也符合协议

@interface RouteChangesInfoViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, RouteChangesInfoTableViewCellDelegate

在viewcontroller内部,我尝试记录文本,但什么也没发生

- (void)textFieldDidChange:(nonnull NSString *)textNote {
    NSLog(@"DEBUG %@:", textNote);
}
  • 这种委托模式正确吗
  • 如果我想观察相关的文本更改,我如何实现观察者(到哪里)

谢谢大家!

您不太了解协议/委托模式

正确定义协议:

@protocol RouteChangesInfoTableViewCellDelegate <NSObject>
@required
- (void)textFieldDidChange:(NSString *)textNote;
@end

RouteChangesInfoTableViewCell.m

//
//  RouteChangesInfoTableViewCell.m
//  Created by Don Mag on 8/29/20.
//

#import "RouteChangesInfoTableViewCell.h"

@implementation RouteChangesInfoTableViewCell

- (IBAction)textFieldDidChange:(id)sender {
    UITextField *tf = (UITextField *)sender;
    [_routeChangesInfoDelegate textFieldDidChange:tf.text];
}

@end
//
//  RouteChangesInfoViewController.m
//  Created by Don Mag on 8/29/20.
//

#import "RouteChangesInfoViewController.h"
#import "RouteChangesInfoTableViewCell.h"

// conform to RouteChangesInfoTableViewCellDelegate
@interface RouteChangesInfoViewController () <RouteChangesInfoTableViewCellDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RouteChangesInfoViewController

// delegate method
- (void)textFieldDidChange:(NSString *)textNote {
    NSLog(@"Got text from cell: %@", textNote);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // make sure table view delegate and datasource are set
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RouteChangesInfoTableViewCell *cell = (RouteChangesInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // set the cell's delegate
    cell.routeChangesInfoDelegate = self;
    
    return cell;
}

@end

RouteChangesInfoViewController.h

//
//  RouteChangesInfoTableViewCell.h
//  Created by Don Mag on 8/29/20.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol RouteChangesInfoTableViewCellDelegate <NSObject>
@required
- (void)textFieldDidChange:(NSString *)textNote;
@end

@interface RouteChangesInfoTableViewCell : UITableViewCell

@property (nonatomic, weak) id <RouteChangesInfoTableViewCellDelegate> routeChangesInfoDelegate;

@end

NS_ASSUME_NONNULL_END
//
//  RouteChangesInfoViewController.h
//  Created by Don Mag on 8/29/20.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface RouteChangesInfoViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

NS_ASSUME_NONNULL_END
//
//RouteChangesInfoViewController.h
//由Don Mag于20年8月29日创建。
//
#进口
NS\u假设\u非空\u开始
@接口路由更改FoviewController:UIViewController
@结束
NS\u假设\u非空\u结束

RouteChangesInfoViewController.m

//
//  RouteChangesInfoTableViewCell.m
//  Created by Don Mag on 8/29/20.
//

#import "RouteChangesInfoTableViewCell.h"

@implementation RouteChangesInfoTableViewCell

- (IBAction)textFieldDidChange:(id)sender {
    UITextField *tf = (UITextField *)sender;
    [_routeChangesInfoDelegate textFieldDidChange:tf.text];
}

@end
//
//  RouteChangesInfoViewController.m
//  Created by Don Mag on 8/29/20.
//

#import "RouteChangesInfoViewController.h"
#import "RouteChangesInfoTableViewCell.h"

// conform to RouteChangesInfoTableViewCellDelegate
@interface RouteChangesInfoViewController () <RouteChangesInfoTableViewCellDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RouteChangesInfoViewController

// delegate method
- (void)textFieldDidChange:(NSString *)textNote {
    NSLog(@"Got text from cell: %@", textNote);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // make sure table view delegate and datasource are set
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RouteChangesInfoTableViewCell *cell = (RouteChangesInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // set the cell's delegate
    cell.routeChangesInfoDelegate = self;
    
    return cell;
}

@end
//
//RouteChangesInfoViewController.m
//由Don Mag于20年8月29日创建。
//
#导入“RouteChangesInfoViewController.h”
#导入“RouteChangesInfoTableViewCell.h”
//符合RouteChangesInfoTableViewCellDelegate
@接口路由更改视野控制器()
@属性(强,非原子)IBUITableView*tableView;
@结束
@实现路由更改FOVIEWController
//委托方法
-(void)textfielddichange:(NSString*)textNote{
NSLog(@“从单元格获取文本:%@”,textNote);
}
-(无效)viewDidLoad{
[超级视图下载];
//确保设置了表视图委托和数据源
_tableView.delegate=self;
_tableView.dataSource=self;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
RouteChangesInfoTableViewCell*单元格=(RouteChangesInfoTableViewCell*)[tableView出列可重用单元格,标识符:@“cell”forIndexPath:indexPath];
//设置单元格的委托
cell.routeChangesInfoDelegate=self;
返回单元;
}
@结束

带有插座连接的故事板…

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="DgD-su-IwN">
    <device id="retina6_1" orientation="portrait" appearance="light"/>
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Route Changes Info View Controller-->
        <scene sceneID="MLL-TB-u9m">
            <objects>
                <viewController id="DgD-su-IwN" customClass="RouteChangesInfoViewController" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="rmO-xF-6kM">
                        <rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="Eu3-I2-qTf">
                                <rect key="frame" x="20" y="144" width="374" height="618"/>
                                <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
                                <prototypes>
                                    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="cell" id="Wqk-Wo-4g3" customClass="RouteChangesInfoTableViewCell">
                                        <rect key="frame" x="0.0" y="28" width="374" height="50.5"/>
                                        <autoresizingMask key="autoresizingMask"/>
                                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Wqk-Wo-4g3" id="kNv-M7-XGt">
                                            <rect key="frame" x="0.0" y="0.0" width="374" height="50.5"/>
                                            <autoresizingMask key="autoresizingMask"/>
                                            <subviews>
                                                <textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="pwd-WX-JE1">
                                                    <rect key="frame" x="8" y="8" width="358" height="34.5"/>
                                                    <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                                    <textInputTraits key="textInputTraits"/>
                                                    <connections>
                                                        <action selector="textFieldDidChange:" destination="Wqk-Wo-4g3" eventType="editingChanged" id="oUL-ZP-fB5"/>
                                                    </connections>
                                                </textField>
                                            </subviews>
                                            <constraints>
                                                <constraint firstItem="pwd-WX-JE1" firstAttribute="top" secondItem="kNv-M7-XGt" secondAttribute="top" constant="8" id="75C-nB-52g"/>
                                                <constraint firstItem="pwd-WX-JE1" firstAttribute="leading" secondItem="kNv-M7-XGt" secondAttribute="leading" constant="8" id="OfP-n0-Ttr"/>
                                                <constraint firstAttribute="bottom" secondItem="pwd-WX-JE1" secondAttribute="bottom" constant="8" id="sqW-Bc-t50"/>
                                                <constraint firstAttribute="trailing" secondItem="pwd-WX-JE1" secondAttribute="trailing" constant="8" id="zrN-ym-Iqw"/>
                                            </constraints>
                                        </tableViewCellContentView>
                                    </tableViewCell>
                                </prototypes>
                            </tableView>
                        </subviews>
                        <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
                        <constraints>
                            <constraint firstItem="2jf-HS-9oh" firstAttribute="bottom" secondItem="Eu3-I2-qTf" secondAttribute="bottom" constant="100" id="GOH-XI-LDv"/>
                            <constraint firstItem="2jf-HS-9oh" firstAttribute="trailing" secondItem="Eu3-I2-qTf" secondAttribute="trailing" constant="20" id="W5X-K6-LdN"/>
                            <constraint firstItem="Eu3-I2-qTf" firstAttribute="leading" secondItem="2jf-HS-9oh" secondAttribute="leading" constant="20" id="a01-1Z-vDi"/>
                            <constraint firstItem="Eu3-I2-qTf" firstAttribute="top" secondItem="2jf-HS-9oh" secondAttribute="top" constant="100" id="b8m-bN-DQ0"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="2jf-HS-9oh"/>
                    </view>
                    <connections>
                        <outlet property="tableView" destination="Eu3-I2-qTf" id="MTy-6L-CMb"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="wqw-h7-jEl" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="150" y="2259"/>
        </scene>
    </scenes>
</document>

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="DgD-su-IwN">
    <device id="retina6_1" orientation="portrait" appearance="light"/>
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Route Changes Info View Controller-->
        <scene sceneID="MLL-TB-u9m">
            <objects>
                <viewController id="DgD-su-IwN" customClass="RouteChangesInfoViewController" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="rmO-xF-6kM">
                        <rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="Eu3-I2-qTf">
                                <rect key="frame" x="20" y="144" width="374" height="618"/>
                                <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
                                <prototypes>
                                    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="cell" id="Wqk-Wo-4g3" customClass="RouteChangesInfoTableViewCell">
                                        <rect key="frame" x="0.0" y="28" width="374" height="50.5"/>
                                        <autoresizingMask key="autoresizingMask"/>
                                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Wqk-Wo-4g3" id="kNv-M7-XGt">
                                            <rect key="frame" x="0.0" y="0.0" width="374" height="50.5"/>
                                            <autoresizingMask key="autoresizingMask"/>
                                            <subviews>
                                                <textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="pwd-WX-JE1">
                                                    <rect key="frame" x="8" y="8" width="358" height="34.5"/>
                                                    <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                                    <textInputTraits key="textInputTraits"/>
                                                    <connections>
                                                        <action selector="textFieldDidChange:" destination="Wqk-Wo-4g3" eventType="editingChanged" id="oUL-ZP-fB5"/>
                                                    </connections>
                                                </textField>
                                            </subviews>
                                            <constraints>
                                                <constraint firstItem="pwd-WX-JE1" firstAttribute="top" secondItem="kNv-M7-XGt" secondAttribute="top" constant="8" id="75C-nB-52g"/>
                                                <constraint firstItem="pwd-WX-JE1" firstAttribute="leading" secondItem="kNv-M7-XGt" secondAttribute="leading" constant="8" id="OfP-n0-Ttr"/>
                                                <constraint firstAttribute="bottom" secondItem="pwd-WX-JE1" secondAttribute="bottom" constant="8" id="sqW-Bc-t50"/>
                                                <constraint firstAttribute="trailing" secondItem="pwd-WX-JE1" secondAttribute="trailing" constant="8" id="zrN-ym-Iqw"/>
                                            </constraints>
                                        </tableViewCellContentView>
                                    </tableViewCell>
                                </prototypes>
                            </tableView>
                        </subviews>
                        <color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
                        <constraints>
                            <constraint firstItem="2jf-HS-9oh" firstAttribute="bottom" secondItem="Eu3-I2-qTf" secondAttribute="bottom" constant="100" id="GOH-XI-LDv"/>
                            <constraint firstItem="2jf-HS-9oh" firstAttribute="trailing" secondItem="Eu3-I2-qTf" secondAttribute="trailing" constant="20" id="W5X-K6-LdN"/>
                            <constraint firstItem="Eu3-I2-qTf" firstAttribute="leading" secondItem="2jf-HS-9oh" secondAttribute="leading" constant="20" id="a01-1Z-vDi"/>
                            <constraint firstItem="Eu3-I2-qTf" firstAttribute="top" secondItem="2jf-HS-9oh" secondAttribute="top" constant="100" id="b8m-bN-DQ0"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="2jf-HS-9oh"/>
                    </view>
                    <connections>
                        <outlet property="tableView" destination="Eu3-I2-qTf" id="MTy-6L-CMb"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="wqw-h7-jEl" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="150" y="2259"/>
        </scene>
    </scenes>
</document>