Ios 与swift文件混淆
在Ios 与swift文件混淆,ios,swift,Ios,Swift,在目标C中,有两个文件.h和.m 例如,在.h文件中,我给出了UIAlertview方法名,如下所示 -(void)showAlertForTitle:(NSString *)strTitle withMessage:(NSString *)strMessage; 在.m文件中,我给出了它的实现,如下所示 - (void)showAlertForTitle:(NSString *)strTitle withMessage:(NSString *)strMessage { [[[UI
目标C
中,有两个文件.h和.m
例如,在.h文件中,我给出了UIAlertview
方法名,如下所示
-(void)showAlertForTitle:(NSString *)strTitle withMessage:(NSString *)strMessage;
在.m
文件中,我给出了它的实现,如下所示
- (void)showAlertForTitle:(NSString *)strTitle withMessage:(NSString *)strMessage
{
[[[UIAlertView alloc] initWithTitle:strTitle message:strMessage delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}
每当我想叫它的时候,我就喜欢
[self showAlertForTitle:@"title" withMessage:@"msg"];
问题:
所以我的问题是…因为只有一个swift文件
。swift
在swift中可用…我是否需要在.swift
文件中给出所有实现,只需导入文件并调用一个方法或以其他方式来实现…在swift中,您不需要这样做,只需以这种方式将函数声明到swift中即可文件:
func showAlertForTitle(strTitle: String, withMessage strMessage: String) {
}
你可以这样称呼它:
showAlertForTitle("title", withMessage: "msg")
编辑:
您的警报功能如下所示:
func showAlertForTitle(strTitle: String, withMessage strMessage: String) {
var alert = UIAlertController(title: strTitle, message: strMessage, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
如果您想将其访问到另一个文件中,可以这样做:
showAlertForTitle("title", withMessage: "msg")
Global.swift
import Foundation
import UIKit
func showAlertForTitle(strTitle: String, withMessage strMessage: String) {
let myAlert = UIAlertView()
myAlert.title = strTitle
myAlert.message = strMessage
myAlert.addButtonWithTitle("Ok")
myAlert.show()
}
现在,您可以使用此功能在项目中的任何位置显示警报。您需要阅读可用的Swift手册。从iBook商店可以免费获得。给你几点钱。首先,如果问题写得不清楚或不符合好问题的标准,则应否决。第二,我没有否决投票或投票结束这个问题。第三,我确实理解这个问题,这就是为什么我建议你阅读现有的文献,因为很明显你没有。