2011-05-17 63 views
3

我的應用程序在MT 3.0中運行良好。現在當我升級。當按鈕位於ContentView中時,我看到錯誤。單擊按鈕時發生崩潰。代碼:ContentView中的按鈕會導致MonoTouch運行時崩潰。 Monotouch 4.0中的錯誤?

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPa 
    float width = tableView.Bounds.Width - 70; 

    var cell = tableView.DequeueReusableCell(kCellIdentifier); 
    //if (cell == null) 
    //{ 
    cell = new UITableViewCell(UITableViewCellStyle.Subtitle, kCellIdentifier); 
    // } 

    var behavior = tvc.behaviors.ElementAt(indexPath.Row); 
    cell.TextLabel.Text = behavior.Name; 
    cell.TextLabel.Font = UIFont.BoldSystemFontOfSize(22f); 
    cell.DetailTextLabel.Text = behavior.Definition; 
    var view = UIButton.FromType(UIButtonType.Custom); 
    view.Tag = indexPath.Row; 
    view.SetImage(UIImage.FromBundle("Images/plus.png"), UIControlState.Normal); 
    view.Frame = new RectangleF(width - 50, 10, 50, 50); 

    view.TouchUpInside += IncrementBehavior; 

    var label = new UILabel(new RectangleF(width - 80, 10, 50, 50)); 
    label.Text = behavior.CurrentCount.ToString(); 
    label.BackgroundColor = UIColor.Clear; 
    label.Font = UIFont.BoldSystemFontOfSize(24f); 
    cell.ContentView.AddSubview(view); 
    cell.ContentView.AddSubview(label); 
    //cell.BackgroundColor = UIColor.Clear;) 

    return cell; 
} 

void IncrementBehavior(object sender, EventArgs e) { 
    var button = (UIButton)sender; 
    var tag = button.Tag; 
    var behavior = tvc.behaviors[tag]; 

    var indexpath = NSIndexPath.FromRowSection(tag, 0); 
    var newBehavior = Repository.GetBehavior(behavior.Id); 
    newBehavior.CurrentCount++; 
    Repository.Update(newBehavior); 
    tvc.behaviors[tag] = newBehavior; 


    tvc.TableView.ReloadRows(new[] { indexpath }, UITableViewRowAnimation.None); 

} 

我得到這些錯誤互換:

Name: NSInvalidArgumentException Reason: -[__NSCFSet BridgeSelector]: unrecognized selector sent to instance 0x5c3c570 

No constructor found for MonoTouch.UIKit.UIControlEventProxy::.ctor(System.IntPtr) 
+1

我希望這裏有人有一個答案,因爲你肯定不會從Attachmate得到任何有用的幫助... – 2011-05-17 22:26:41

+1

你介意給我提供一個自包含的測試用例,展示這個問題嗎? – 2011-07-17 02:55:22

+0

當我爲touchupinside分配相同的雙處理按鈕時,發生了一些非常類似於我的事情。 – 2014-01-27 18:47:30

回答

10

不知道這是一個問題,但是當我升級到4.0,我也得到了一些隨機崩潰。事實證明,4.0 GC更具侵略性,而我以前逃避的事情不再是猶太教。

特別是,如果我有一個事件處理程序分配給一個按鈕,我需要確保按鈕是在類級別聲明的。如果它是在方法中本地聲明的,那麼GC會在引用超出範圍時清除引用,然後在事件處理程序試圖引發時,引用不再存在。

因此,請嘗試在您的方法之外移動按鈕的聲明。

+0

那麼我每行都有一個按鈕,我應該將它存儲在某種集合中嗎? – 2011-05-17 22:41:31

+0

完美的工作!謝謝......雖然這非常令人討厭。我到處都有這種代碼。任何其他方式來做到這一點? – 2011-05-17 22:47:28

+0

這是我意識到的唯一方式,並且考慮到當前MT的非狀態,我懷疑有更好的解決方法或解決方法即將出現。 – Jason 2011-05-17 23:41:21