如何设置Swift中自定义UIBarButtonItem的操作?
以下代码成功将按钮放在导航栏中:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil)
self.navigationItem.rightBarButtonItem = b
Run Code Online (Sandbox Code Playgroud)
现在,我想func sayHello() { println("Hello") }在触摸按钮时打电话.到目前为止我的努力:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:sayHello:)
// also with `sayHello` `sayHello()`, and `sayHello():`
Run Code Online (Sandbox Code Playgroud)
和..
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(sayHello:))
// also with `sayHello` `sayHello()`, and `sayHello():`
Run Code Online (Sandbox Code Playgroud)
和..
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(self.sayHello:))
// also with `self.sayHello` `self.sayHello()`, and `self.sayHello():`
Run Code Online (Sandbox Code Playgroud)
请注意,它sayHello()出现在intellisense中,但不起作用.
谢谢你的帮助.
编辑:对于后代,以下工作: …
我可以找到关于Func <>和Action <>的所有示例都很简单,如下所示,您可以看到它们在技术上如何工作,但我希望看到它们用于解决以前无法解决的问题的示例中只有在一个更复杂的方式,即我知道他们是如何工作的,我可以看到他们是可以解决简洁,功能强大,所以我想了解他们在更大的意义上他们解决什么样的问题,我怎么可能会在使用它们应用程序设计.
您以什么方式(模式)使用Func <>和Action <>来解决实际问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestFunc8282
{
class Program
{
static void Main(string[] args)
{
//func with delegate
Func<string, string> convert = delegate(string s)
{
return s.ToUpper();
};
//func with lambda
Func<string, string> convert2 = s => s.Substring(3, 10);
//action
Action<int,string> recordIt = (i,title) =>
{
Console.WriteLine("--- {0}:",title);
Console.WriteLine("Adding five to {0}:", i);
Console.WriteLine(i + 5);
};
Console.WriteLine(convert("This is the first test.")); …Run Code Online (Sandbox Code Playgroud) 今天我在考虑宣布这个:
private delegate double ChangeListAction(string param1, int number);
Run Code Online (Sandbox Code Playgroud)
但为什么不用这个:
private Func<string, int, double> ChangeListAction;
Run Code Online (Sandbox Code Playgroud)
或者如果ChangeListAction没有返回值,我可以使用:
private Action<string,int> ChangeListAction;
Run Code Online (Sandbox Code Playgroud)
那么使用delegate关键字声明委托的优势在哪里?
是因为.NET 1.1,随着.NET 2.0的出现Action<T>和.NET 3.5的出现Func<T>?
private void Include(IList<string> includes, Action action)
{
if (includes != null)
{
foreach (var include in includes)
action(<add include here>);
}
}
Run Code Online (Sandbox Code Playgroud)
我想这样称呼它
this.Include(includes, _context.Cars.Include(<NEED TO PASS each include to here>));
Run Code Online (Sandbox Code Playgroud)
这个想法是将每个包含传递给方法.
我正在使用SignalR的中心功能(https://github.com/SignalR/SignalR)向所有订阅的客户端发布消息:
public class NewsFeedHub : Hub
public void Send(string channel, string content)
{
Clients[channel].addMessage(content);
}
Run Code Online (Sandbox Code Playgroud)
这通过Javascript调用"发送"时工作正常,但我也希望Web应用程序发布消息(来自ASP.NET MVC操作方法).我已经尝试实例化新对象ob NewsFeedHub并调用Send方法,但这会导致错误(因为未设置Hub的基础"连接").有没有办法在没有连接的情况下使用Hub?
假设我有一个文章模型,在文章"新"视图中我有两个按钮,"发布"和"保存草稿".
我的问题是如何知道在控制器中单击了哪个按钮.
我已经有了解决方案,但我认为必须有更好的方法.我目前在视图中使用的是:
<div class="actions">
<%= f.submit "Publish" %>
<%= f.submit "Save Draft", :name => "commit" %>
</div>
Run Code Online (Sandbox Code Playgroud)
所以在控制器中,我可以使用params [:commit]字符串来处理该操作.
def create
@article = Article.new(params[:article])
if params[:commit] == "Publish"
@article.status = 'publish'
// detail omitted
end
@article.save
end
Run Code Online (Sandbox Code Playgroud)
但我认为使用视图相关字符串并不好.你能告诉我另一种方法吗?
更新:由于这些按钮的形式相同,所以它们都会进入"创建"动作,这对我来说没问题.我想要的是在创建操作中处理它,例如给Article模型一个'status'列并保持'public'或'draft'.
在ASP.NET MVC的时候我的行动将不会返回任何东西我使用return new EmptyResult()
或return null
有什么不同吗?
有没有办法在加载页面时执行JSF托管bean操作?
如果那是相关的,我现在正在使用JSF 1.2.
我有一个带有两个按钮和几个输入字段的jsf 1.2表单.第一个按钮放弃输入的值,并使用db中的值重新填充页面,第二个按钮保存输入的值.当用户在光标位于其中一个输入字段中时按下enter键,表单被提交并且与第一个按钮相关联的操作被执行时,会出现问题.
代码如下所示:
<h:commandButton action="#{bean.reset}" value="Reset" />
<h:commandButton action="#{bean.save}" value="Save" />
<!-- h:datatable with several h:inputText elements -->
Run Code Online (Sandbox Code Playgroud)
按Enter键是否可以将特定按钮声明为默认操作?这种行为实际上是在某处指定的吗?
作为一个刚刚开始使用MVC的相当有经验的ASP.Net开发人员,我发现自己正在努力将我的思维方式从传统的"服务器控件和事件处理程序"的处理方式转变为更加动态的MVC方式.我想我正在慢慢地到达那里,但有时MVC"魔法"会让我失望.
我目前的情况是创建一个网页,允许用户浏览本地文件,将其上传到服务器并重复此操作,直到他有一个要使用的文件列表.当他对文件列表(将在页面上的网格中显示)感到满意时,他将单击一个按钮来处理文件并提取将存储在数据库中的一些数据.
最后一部分并不是那么重要,现在我正在努力处理像构建文件列表这样简单的事情,并在请求之间保持该列表.在传统方法中,这将非常简单 - 数据将保存在ViewState中.但是在MVC中我需要在控制器和视图之间传递数据,而我并没有完全了解它应该如何工作.
我想我最好发布我的相当不完整的编码尝试来解释这个问题.
为了保存我的文件列表数据,我创建了一个viewmodel,它基本上是一个类型化的文件列表,还有一些额外的元数据:
public class ImportDataViewModel
{
public ImportDataViewModel()
{
Files = new List<ImportDataFile>();
}
public List<ImportDataFile> Files { get; set; }
...
Run Code Online (Sandbox Code Playgroud)
在视图中,我有一个用于浏览和上传文件的表单:
<form action="AddImportFile" method="post" enctype="multipart/form-data">
<label for="file">
Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
该视图使用viewmodel作为其模型:
@model MHP.ViewModels.ImportDataViewModel
Run Code Online (Sandbox Code Playgroud)
这会将文件发送到我的操作:
public ActionResult AddImportFile(HttpPostedFileBase file, ImportDataViewModel importData)
{
if (file.ContentLength > 0)
{
ImportDataFile idFile = new ImportDataFile { File = file };
importData.Files.Add(idFile);
}
return View("DataImport", importData);
}
Run Code Online (Sandbox Code Playgroud)
此操作返回DataImport页面的视图以及包含文件列表的viewmodel实例.
这可以很好地工作到某一点,我可以浏览文件并上传它,我可以在动作中看到viewmodel数据,然后如果我在视图中放置一个断点并调试"this.Model",一切都是精细.
但是,如果我尝试上传另一个文件,在AddImportFile操作中放置断点时,importData参数为空.因此,视图显然没有将其模型的当前实例传递给操作.
在我经历的MVC示例中,模型实例被"神奇地"传递给action方法作为参数,那么为什么它现在为空呢? …
action ×10
asp.net-mvc ×3
c# ×3
delegates ×2
func ×2
jsf ×2
.net ×1
default ×1
enter ×1
form-submit ×1
forms ×1
ios ×1
managed-bean ×1
message ×1
onload ×1
signalr ×1
signalr-hub ×1
state ×1
swift ×1
viewmodel ×1