Skip to content

Instantly share code, notes, and snippets.

@Clancey
Last active December 30, 2015 05:29
Show Gist options
  • Save Clancey/7783148 to your computer and use it in GitHub Desktop.
Save Clancey/7783148 to your computer and use it in GitHub Desktop.
I was tired of doing switch/case statements whenever I used a UIAction sheet. I also didn't like that I couldn't change the color of a specific button. So Simple Action Sheet was born.
/*
var options = new [] {
Priority.Low,
Priority.Normal,
Priority.High,
};
var sheet = new SimpleActionSheet {
Title = file.ToString()
};
foreach (var p in options)
sheet.AddButton (p.ToString (),UIColor.Green, ()=> file.Priority = p);
sheet.AddButton ("Best button", () => {
// Do something awesome!
});
sheet.DestructiveButtonIndex = sheet.AddButton ("Cancel");
sheet.ShowInView(this.View);
*/
public class SimpleActionSheet : UIActionSheet
{
Dictionary<int,Action> dict = new Dictionary<int, Action>();
Dictionary<int,UIColor> colors = new Dictionary<int, UIColor> ();
public SimpleActionSheet ()
{
Clicked += (object sender, UIButtonEventArgs e) => {
Action a;
if (dict.TryGetValue (e.ButtonIndex, out a) && a != null)
a ();
};
WillPresent += (object sender, EventArgs e) => {
foreach(UIButton b in Subviews.Where(x=> x is UIButton))
{
UIColor color;
if(!colors.TryGetValue(b.Tag -1,out color))
return;
b.SetTitleColor(color, UIControlState.Normal);
}
};
}
public int AddButton(string title, Action action)
{
var index = AddButton (title);
dict.Add (index, action);
return index;
}
public int AddButton(string title, UIColor color, Action action)
{
var index = AddButton (title);
dict.Add (index, action);
colors.Add(index,color);
return index;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment